我想獲得一個用戶的所有用戶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