我的content表里面有100万条数据
我用下面的脚本
SELECT COUNT(*)
FROM shinyv_content
WHERE sectionid IN (85, 86) WHERE state = 1 ;
运行时间很长,我看了一下,大部分用于Sending data 73.837627 。我explain后显示:
1 SIMPLE shinyv_content index_merge idx_section,idx_state idx_section,idx_state 5,2 NULL 32462 Using intersect(idx_section,idx_state); Using where; Using index我不知道为什么会这么慢,谁能解释一下?
我用下面的脚本
SELECT COUNT(*)
FROM shinyv_content
WHERE sectionid IN (85, 86) WHERE state = 1 ;
运行时间很长,我看了一下,大部分用于Sending data 73.837627 。我explain后显示:
1 SIMPLE shinyv_content index_merge idx_section,idx_state idx_section,idx_state 5,2 NULL 32462 Using intersect(idx_section,idx_state); Using where; Using index我不知道为什么会这么慢,谁能解释一下?
这里应该是语法出错啊。你的能够执行?
FROM shinyv_content
WHERE sectionid IN (85, 86) WHERE state = 1 这句SQL能执行?
1、索引情况;
2、语法问题;
3、sectionid IN (85, 86)有多少条记录 ,state = 1有多少条记录,state 的值只有0、1?
SQL语句是这样的:
SELECT COUNT(*)
FROM shinyv_content
WHERE sectionid IN (85, 86) AND state = 1 ;sectionid 和 state 都是索引
但是state 只有 0,1 (基本上都是1)
sectionid IN (85, 86)一共有4万多条整张表有100万条记录,就是这样的SQL语句运行要1分多钟。怎么优化呀?
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment
shinyv_content 0 PRIMARY 1 id A 1002256 NULL NULL BTREE
shinyv_content 1 idx_section 1 sectionid A 25 NULL NULL YES BTREE
shinyv_content 1 idx_checkout 1 checked_out A 1 NULL NULL YES BTREE
shinyv_content 1 idx_state 1 state A 1 NULL NULL YES BTREE
shinyv_content 1 idx_mask 1 mask A 1 NULL NULL YES BTREE
shinyv_content 1 hits 1 hits A 1002256 NULL NULL YES BTREE
shinyv_content 1 ordering 1 ordering A 1002256 NULL NULL YES BTREE
shinyv_content 1 created 1 created A 64 NULL NULL YES BTREE
FROM shinyv_content
WHERE sectionid IN (85, 86) AND state = 1 ;
在sectionid、state建立复合索引试试