我想获得一个用户的所有用户SQL查询太慢甚至 “使用索引” 解释
我的查询:
SELECT
COUNT(sub.id) as ids
FROM
subscribers as sub
WHERE
suid=541839243781
EXPLAIN打印:
╔════╦═════════════╦═══════╦══════╦═══════════════╦═════╦═════════╦═══════╦═══════╦═════════════╗ ║ id ║ select_type ║ table ║ type ║ possible_keys ║ key ║ key_len ║ ref ║ rows ║ Extra ║ ╠════╬═════════════╬═══════╬══════╬═══════════════╬═════╬═════════╬═══════╬═══════╬═════════════╣ ║ 1 ║ SIMPLE ║ sub ║ ref ║ i3 ║ i3 ║ 8 ║ const ║ 47890 ║ Using index ║ ╚════╩═════════════╩═══════╩══════╩═══════════════╩═════╩═════════╩═══════╩═══════╩═════════════╝
因此在当我总得到的总数是48k
,它需要0.0333
加载......如果这个数字上升到1m
或5m
?那么它可能需要年龄加载了......
我的用户表索引是:
╔═════════════╦════════════╦═══════════════════╦══════════════╦═════════════╦═══════════╦═════════════╦══════════╦════════╦══════╦════════════╦═════════╗ ║ Table ║ Non_unique ║ Key_name ║ Seq_in_index ║ Column_name ║ Collation ║ Cardinality ║ Sub_part ║ Packed ║ Null ║ Index_type ║ Comment ║ ╠═════════════╬════════════╬═══════════════════╬══════════════╬═════════════╬═══════════╬═════════════╬══════════╬════════╬══════╬════════════╬═════════╣ ║ subscribers ║ 0 ║ PRIMARY ║ 1 ║ id ║ A ║ 60251 ║ NULL ║ NULL ║ ║ BTREE ║ ║ ║ subscribers ║ 1 ║ total_subscribers ║ 1 ║ id ║ A ║ 60251 ║ NULL ║ NULL ║ ║ BTREE ║ ║ ║ subscribers ║ 1 ║ total_subscribers ║ 2 ║ suid ║ A ║ 60251 ║ NULL ║ NULL ║ ║ BTREE ║ ║ ║ subscribers ║ 1 ║ i3 ║ 1 ║ suid ║ A ║ 6025 ║ NULL ║ NULL ║ ║ BTREE ║ ║ ║ subscribers ║ 1 ║ i3 ║ 2 ║ uid ║ A ║ 60251 ║ NULL ║ NULL ║ ║ BTREE ║ ║ ║ subscribers ║ 1 ║ i3 ║ 3 ║ id ║ A ║ 60251 ║ NULL ║ NULL ║ ║ BTREE ║ ║ ╚═════════════╩════════════╩═══════════════════╩══════════════╩═════════════╩═══════════╩═════════════╩══════════╩════════╩══════╩════════════╩═════════╝
所以我怎样才能让这个查询更有效?
如果我正确读取数据,则没有“suid”列的专用索引。 “suid”列值是全球唯一还是仅针对每个订阅者? – 2012-04-17 17:27:12
已编辑的索引,忘记了一个索引 – fxuser 2012-04-17 17:33:00