Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Query too slow even "Using Index" on EXPLAIN
    text
    copied!<p>i am trying to get all subscribers of a user</p> <p>my query:</p> <pre><code>SELECT COUNT(sub.id) as ids FROM subscribers as sub WHERE suid=541839243781 </code></pre> <p>EXPLAIN prints:</p> <pre> ╔════╦═════════════╦═══════╦══════╦═══════════════╦═════╦═════════╦═══════╦═══════╦═════════════╗ ║ id ║ select_type ║ table ║ type ║ possible_keys ║ key ║ key_len ║ ref ║ rows ║ Extra ║ ╠════╬═════════════╬═══════╬══════╬═══════════════╬═════╬═════════╬═══════╬═══════╬═════════════╣ ║ 1 ║ SIMPLE ║ sub ║ ref ║ i3 ║ i3 ║ 8 ║ const ║ 47890 ║ Using index ║ ╚════╩═════════════╩═══════╩══════╩═══════════════╩═════╩═════════╩═══════╩═══════╩═════════════╝ </pre> <p>so at the moment the total count i get is around <code>48k</code> and it takes <code>0.0333</code> to load... what if this goes up to <code>1m</code> or <code>5m</code> ?? then it could take ages to load up...</p> <p>my indexes on subscribers table are:</p> <pre> ╔═════════════╦════════════╦═══════════════════╦══════════════╦═════════════╦═══════════╦═════════════╦══════════╦════════╦══════╦════════════╦═════════╗ ║ 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 ║ ║ ╚═════════════╩════════════╩═══════════════════╩══════════════╩═════════════╩═══════════╩═════════════╩══════════╩════════╩══════╩════════════╩═════════╝ </pre> <p>so how can i make this query more efficient?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload