[Solved] MySQL not using my index

EverSQL Database Performance Knowledge Base

MySQL not using my index

Database type:

I have a table like this:

CREATE TABLE `entries` (
  `n` int(10) unsigned NOT NULL DEFAULT '0',
  `title` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(255) NOT NULL DEFAULT '',
  `c` char(6) DEFAULT NULL,
  `c_path` char(24) DEFAULT NULL,
  `a` char(8) DEFAULT NULL,
  `a_path` char(32) DEFAULT NULL,
  `hide_in_supercat` tinyint(1) NOT NULL DEFAULT '0',
  `created` int(10) unsigned NOT NULL DEFAULT '0',
  `edited` int(10) unsigned NOT NULL DEFAULT '0',
  `t1` int(10) unsigned NOT NULL DEFAULT '0',
  `t2` int(10) unsigned NOT NULL DEFAULT '0',
  `status` set('waiting','enabled','disabled','queue','wait','preview') NOT NULL DEFAULT '',
  `clicks_total` int(10) unsigned NOT NULL DEFAULT '0',
  `popular` tinyint(1) NOT NULL DEFAULT '0',
  `featured_until` int(10) unsigned NOT NULL DEFAULT '0',
  `home_page_until` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`n`,`t1`,`t2`,`status`,`en_cats`),
  KEY `category_page_index` (`c`,`c_path`,`hide_in_supercat`,`featured_until`,`highlight_until`),
  KEY `category_page_index_2` (`c`,`c_path`,`hide_in_supercat`,`featured_until`,`highlight_until`,`created`,`clicks_total`),
  FULLTEXT KEY `search` (`title`,`description`,`detail`,`keywords`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 

And I'm trying to run the following query:

select sql_no_cache * from gc_ads where c_path in ("_9_","_9_30_","_9_101_","_9_102_","_9_103_","_9_105_","_9_106_","_9_30_132_","_9_30_133_","_9_30_134_","_9_30_135_","_9_30_136_","_9_30_137_","_9_30_138_","_9_30_139_","_9_30_140_","_9_30_141_","_9_30_142_","_9_30_143_","_9_30_144_","_9_30_145_","_9_30_146_","_9_30_147_","_9_30_148_","_9_30_149_","_9_30_150_","_9_30_151_","_9_30_152_","_9_30_153_","_9_30_154_","_9_30_155_","_9_30_156_","_9_30_157_","_9_30_158_","_9_30_159_","_9_30_160_","_9_30_161_","_9_30_162_","_9_101_222_","_9_101_221_","_9_101_220_","_9_101_219_","_9_101_218_","_9_101_217_","_9_101_216_","_9_101_215_","_9_101_214_","_9_101_213_","_9_101_212_","_9_101_211_","_9_101_210_","_9_101_209_","_9_101_208_","_9_101_207_","_9_101_206_","_9_101_205_","_9_101_204_","_9_101_203_","_9_101_202_","_9_101_201_","_9_101_200_","_9_101_199_","_9_101_198_","_9_101_196_","_9_101_197_","_9_101_195_","_9_194_","_9_101_223_","_9_101_224_","_9_101_225_","_9_102_226_","_9_102_227_","_9_102_228_","_9_102_229_","_9_102_230_","_9_102_231_","_9_102_232_","_9_102_233_","_9_102_234_","_9_102_235_","_9_102_236_","_9_102_237_","_9_102_238_","_9_102_239_","_9_102_240_","_9_102_241_","_9_102_242_","_9_102_243_","_9_102_244_","_9_102_245_","_9_102_246_","_9_102_247_","_9_102_248_","_9_102_249_","_9_102_250_","_9_102_251_","_9_102_252_","_9_102_253_","_9_102_254_","_9_102_255_","_9_102_256_") AND status = 'enabled' and (hide_in_supercat!=1 or c='_9_') order by created desc limit 0,50;

Explain gives me the following:

    +----+-------------+--------+------+-------------------------------------------+------+---------+------+-------+-----------------------------+
| id | select_type | table  | type | possible_keys                             | key  | key_len | ref  | rows  | Extra                       |
+----+-------------+--------+------+-------------------------------------------+------+---------+------+-------+-----------------------------+
|  1 | SIMPLE      | gc_ads | ALL  | category_page_index,category_page_index_2 | NULL | NULL    | NULL | 60554 | Using where; Using filesort |
+----+-------------+--------+------+-------------------------------------------+------+---------+------+-------+-----------------------------+

I'm rather new to MySQL and indexes, so this makes no sense. I'm sure it's probably some simple concept that I'm missing.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid OFFSET In LIMIT Clause (query line: 15): OFFSET clauses can be very slow when used with high offsets (e.g. with high page numbers when implementing paging). Instead, use the following \u003ca target\u003d"_blank" href\u003d"http://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/"\u003eseek method\u003c/a\u003e, which provides better and more stable response rates.
  2. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  3. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `gc_ads` ADD INDEX `gc_ads_idx_status_c_path_created` (`status`,`c_path`,`created`);
ALTER TABLE `gc_ads` ADD INDEX `gc_ads_idx_created` (`created`);
The optimized query:
SELECT
        SQL_NO_CACHE * 
    FROM
        gc_ads 
    WHERE
        gc_ads.c_path IN (
            '_9_', '_9_30_', '_9_101_', '_9_102_', '_9_103_', '_9_105_', '_9_106_', '_9_30_132_', '_9_30_133_', '_9_30_134_', '_9_30_135_', '_9_30_136_', '_9_30_137_', '_9_30_138_', '_9_30_139_', '_9_30_140_', '_9_30_141_', '_9_30_142_', '_9_30_143_', '_9_30_144_', '_9_30_145_', '_9_30_146_', '_9_30_147_', '_9_30_148_', '_9_30_149_', '_9_30_150_', '_9_30_151_', '_9_30_152_', '_9_30_153_', '_9_30_154_', '_9_30_155_', '_9_30_156_', '_9_30_157_', '_9_30_158_', '_9_30_159_', '_9_30_160_', '_9_30_161_', '_9_30_162_', '_9_101_222_', '_9_101_221_', '_9_101_220_', '_9_101_219_', '_9_101_218_', '_9_101_217_', '_9_101_216_', '_9_101_215_', '_9_101_214_', '_9_101_213_', '_9_101_212_', '_9_101_211_', '_9_101_210_', '_9_101_209_', '_9_101_208_', '_9_101_207_', '_9_101_206_', '_9_101_205_', '_9_101_204_', '_9_101_203_', '_9_101_202_', '_9_101_201_', '_9_101_200_', '_9_101_199_', '_9_101_198_', '_9_101_196_', '_9_101_197_', '_9_101_195_', '_9_194_', '_9_101_223_', '_9_101_224_', '_9_101_225_', '_9_102_226_', '_9_102_227_', '_9_102_228_', '_9_102_229_', '_9_102_230_', '_9_102_231_', '_9_102_232_', '_9_102_233_', '_9_102_234_', '_9_102_235_', '_9_102_236_', '_9_102_237_', '_9_102_238_', '_9_102_239_', '_9_102_240_', '_9_102_241_', '_9_102_242_', '_9_102_243_', '_9_102_244_', '_9_102_245_', '_9_102_246_', '_9_102_247_', '_9_102_248_', '_9_102_249_', '_9_102_250_', '_9_102_251_', '_9_102_252_', '_9_102_253_', '_9_102_254_', '_9_102_255_', gc_ads."_9_102_256_"
        ) 
        AND gc_ads.status = 'enabled' 
        AND (
            gc_ads.hide_in_supercat != 1 
            OR gc_ads.c = '_9_'
        ) 
    ORDER BY
        gc_ads.created DESC LIMIT 0,
        50

Related Articles



* original question posted on StackOverflow here.