Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL How can I speed up this SELECT query?
    primarykey
    data
    text
    <p>I have a very large table of over 9 million rows and in my software I need to rapidly do select queries on it in a loop. The problem is that the queries take almost 4 seconds to complete. Here is one of the queries (they are all similar in that they all have the same WHERE clause that is slowing them down:</p> <pre><code>SELECT MIN(id) FROM `04c1Tg0M` WHERE `tried` = 0; </code></pre> <p>I'm using the tried column as a boolean. the value is either 1 or 0. Here is the EXPLAIN from that query:</p> <pre><code>--------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+------+---------+------+---------+--------------------------+ | 1 | SIMPLE | 04c1Tg0M | index | NULL | pdex | 158 | NULL | 9275107 | Using where; Using index | +----+-------------+----------+-------+---------------+------+---------+------+---------+--------------------------+ </code></pre> <p>Here is the table structure:</p> <pre><code>CREATE TABLE `04c1Tg0M` ( `id` int(20) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `tried` tinyint(1) DEFAULT '0', PRIMARY KEY (`id`), KEY `pdex` (`username`,`id`,`tried`) ) ENGINE=MyISAM AUTO_INCREMENT=9275108 DEFAULT CHARSET=utf8 </code></pre> <p>Here is the output of show indes:</p> <pre><code>| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ | 04c1Tg0M | 0 | PRIMARY | 1 | id | A | 9275107 | NULL | NULL | | BTREE | | | 04c1Tg0M | 1 | pdex | 1 | username | A | 9275107 | NULL | NULL | | BTREE | | | 04c1Tg0M | 1 | pdex | 2 | id | A | 9275107 | NULL | NULL | | BTREE | | | 04c1Tg0M | 1 | pdex | 3 | tried | A | 9275107 | NULL | NULL | YES | BTREE | | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ </code></pre> <p>And here is the output from the query in question:</p> <pre><code>+---------+ | MIN(id) | +---------+ | 1 | +---------+ 1 row in set (3.76 sec) </code></pre> <p>I need to drastically reduce the query time. Any help is greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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