請教各位大神,在主從中,相同表結構,相同資料,同一個sql,從庫走索引,主庫就不走索引。
show index from tr_order 命令查看索引都是生效的,并沒有被關閉。
在從庫用exlpain執行計劃看sql,走索引。
在主庫用exlpain執行計劃看sql,不走索引。
請問各位大神有遇到這樣的問題嗎?該如何解決。
uj5u.com熱心網友回復:
也許是統計資訊不一樣。如果確定走索引更快的話,用force index系結一下吧
uj5u.com熱心網友回復:
用force index也試過, 他強制走索引了,但是row的數量跟沒走索引的數量是一樣的。uj5u.com熱心網友回復:
叢庫走索引 row是16 主庫沒走索引row是30萬 主庫force index強制走索引 key的顯示的是索引,但是row還是30萬uj5u.com熱心網友回復:
表結構,索引情況,執行計劃,方便的話都貼出來看看,資訊太少難以判斷uj5u.com熱心網友回復:
主從的表結構:CREATE TABLE `tr_order` (
`ctb_order_id` bigint(20) NOT NULL AUTO_INCREMENT,
`order_no` char(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '13位的時間戳 + 自增號段(3位)+ 本機IP(3位)+ PID(3位) + 資料型別(2位)+ 幣種ID(3位)+ 亂數(5位)',
`customer_uuid` char(32) COLLATE utf8mb4_bin NOT NULL COMMENT '[用戶關聯ID]',
`currency_id` int(11) NOT NULL COMMENT '[幣種ID]',
`base_currency_id` int(11) NOT NULL COMMENT '[基礎幣ID]',
`buyOrSell` int(11) DEFAULT NULL COMMENT '[買賣方向] 1. 買入 2.賣出',
`type` int(11) DEFAULT NULL COMMENT '[委托類別] 1. 限價 2. 市價',
`num` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[委托數量]',
`trade_num` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[成交數量]',
`cancel_num` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[撤單數量]',
`remain_num` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[未成交數量] 部分成交之后剩余的數量,初始值等于委托數量 remain_num = num-trade_num-cacel_num',
`price` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[委托價格]',
`source` int(11) DEFAULT '1' COMMENT '[委托來源] 1. web瀏覽器(默認)',
`order_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '[委托時間] 目前等于Create_Time',
`fee` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[委托手續費] buyOrSell 為1時:標識為數字貨幣 2:時標識為人民幣',
`freeze_fee` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[凍結手續費] 暫時為預留欄位',
`freeze_amount` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[凍結金額]',
`risk_flag` int(11) DEFAULT '1' COMMENT '[風控標示] 1. 無風控預警 2. 風控預警',
`cancel_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '[撤單時間]',
`average_price` decimal(32,16) DEFAULT '0.0000000000000000' COMMENT '[平均成交價] 有成交后回寫',
`status` int(11) NOT NULL DEFAULT '0' COMMENT '[狀態] 0. 未成交 1. 部分成交 2. 全部成交 3. 委托失敗 4. 全部撤單 5. 部分成交后撤單',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`create_by` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
`last_edit_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`last_edit_by` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`ctb_order_id`),
UNIQUE KEY `order_no_idx` (`order_no`),
KEY `order_cuuuid_indx` (`customer_uuid`) USING HASH,
KEY `order_currency_indx` (`currency_id`) USING HASH,
KEY `order_basecurrency_indx` (`base_currency_id`) USING HASH,
KEY `status_index` (`status`),
KEY `link_index` (`customer_uuid`,`currency_id`,`base_currency_id`,`status`),
KEY `ordertime` (`order_time`)
) ENGINE=InnoDB AUTO_INCREMENT=407749478 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='委托單表';
sql:
EXPLAIN
SELECT
order_no,
customer_uuid,
currency_id,
base_currency_id,
buyOrSell,
type,
num,
trade_num,
cancel_num,
remain_num,
price,
order_time,
fee,
risk_flag,
cancel_time,
average_price,
STATUS
FROM
tr_order
WHERE
1 = 1
AND customer_uuid = '3462a7a8845741089e628f2804be5c2b'
AND currency_id = 219
AND base_currency_id = 63
AND STATUS IN (0, 1)
AND type = 1
AND order_time >= '2019-03-14'
ORDER BY
ctb_order_id DESC
LIMIT 0,
50;
主庫執行計劃:

從庫執行計劃

uj5u.com熱心網友回復:
WHERE1 = 1
AND customer_uuid = '3462a7a8845741089e628f2804be5c2b'
AND currency_id = 219
AND base_currency_id = 63
AND STATUS IN (0, 1)
AND type = 1
AND order_time >= '2019-03-14'
這個where條件總共回傳多少行?
貼下這個資訊:
show index from tr_order;
uj5u.com熱心網友回復:
這個是主庫 也就是不走索引的庫
uj5u.com熱心網友回復:
從索引情況來看,主庫應該系結索引link_index ,force index(link_index)你對比下force index(link_index)和force index(order_cuuuid_index)的執行計劃和時間
uj5u.com熱心網友回復:
找到原因了嗎?我解決了,但是沒有找到原因轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/43475.html
標籤:MySQL
