[Solved] Mysql Performance of rrder by a date field
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Mysql Performance of rrder by a date field

Database type:

I have the following MySql query

SELECT DISTINCT cats.name AS cat_name, cats.alias AS cat_alias, content.catid AS cat_id, content.title AS title, content.introtext AS text, content.created AS date, content.publish_up AS date_publish, content.id AS ID, content.alias AS alias, content.hits
    AS hits, content.plugins AS plugins 
FROM t0nbl_k2_items AS content LEFT JOIN t0nbl_k2_categories AS cats ON cats.id = content.catid 
WHERE content.trash = 0 
AND cats.access <=3 
AND content.access <=3 
AND content.published=1 
AND cats.published=1 
AND ( content.publish_up='0000-00-00 00:00:00' OR content.publish_up <='2014-10-31 00:00:00' ) 
AND ( content.publish_down='0000-00-00 00:00:00' OR content.publish_down>= '2014-10-31 00:00:00' ) 
ORDER BY content.created ASC 
LIMIT 0,12;

It takes 4 second to get the result.

The explain statement shows the following

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  content     range   item,catid,item_categ,latest    item    10  NULL    11858   Using index condition; Using where; Using temporary; Using filesort
1   SIMPLE  cats    eq_ref  PRIMARY,category,published,access,item_categ    PRIMARY     4   site2.content.catid     1   Using where

and here are the indexes.

INDEXNAME   FIELDNAME
PRIMARY     id
item        published
            publish_up
            publish_down
            trash
            access
catid       catid
created_by  created_by
ordering    ordering
featured    featured
created     created 
language    language
item_categ  access  
            published
            publish_up  
            publish_down
            catid
            created
latest  publish_up  
        publish_down
        created 

I don't know how to optimize this query to speed it. the #_k2_items has 30.000 items. as you see it is not so big data. So which indexes i should define to let Mysql use them?

Sorry for my bad english and formating the codes. I am new here to use this editor.

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. 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.
  2. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `t0nbl_k2_categories`) are used in the 'where' clause, in a way that allows to replace it with an optimized inner join. Inner joins can be fully optimized by the database, while Left joins apply limitations on the database's optimizer.
  3. Sort and Limit Before Joining (modified query below): In cases where the joins aren't filtering any rows, it's possible to sort and limit the amount of rows using a subquery in the FROM clause, before applying the joins to all other tables.
Optimal indexes for this query:
ALTER TABLE `t0nbl_k2_categories` ADD INDEX `t0nbl_categories_idx_published_access` (`published`,`access`);
ALTER TABLE `t0nbl_k2_items` ADD INDEX `t0nbl_items_idx_trash_publis_publis_publis_access` (`trash`,`published`,`publish_up`,`publish_down`,`access`);
ALTER TABLE `t0nbl_k2_items` ADD INDEX `t0nbl_items_idx_created` (`created`);
The optimized query:
SELECT
        DISTINCT cats.name AS cat_name,
        cats.alias AS cat_alias,
        cat_id AS cat_id,
        title AS title,
        text AS text,
        date AS date,
        date_publish AS date_publish,
        ID AS ID,
        alias AS alias,
        hits AS hits,
        plugins AS plugins 
    FROM
        (SELECT
            content.catid AS cat_id,
            content.title AS title,
            content.introtext AS text,
            content.created AS date,
            content.publish_up AS date_publish,
            content.id AS ID,
            content.alias AS alias,
            content.hits AS hits,
            content.plugins AS plugins 
        FROM
            t0nbl_k2_items AS content 
        WHERE
            (
                (
                    (
                        (
                            content.trash = 0
                        ) 
                        AND (
                            content.access <= 3
                        )
                    ) 
                    AND (
                        content.published = 1
                    )
                ) 
                AND (
                    content.publish_up = '0000-00-00 00:00:00' 
                    OR content.publish_up <= '2014-10-31 00:00:00'
                )
            ) 
            AND (
                content.publish_down = '0000-00-00 00:00:00' 
                OR content.publish_down >= '2014-10-31 00:00:00'
            ) 
        ORDER BY
            content.created ASC LIMIT 12) AS content 
    INNER JOIN
        t0nbl_k2_categories AS cats 
            ON cats.id = content.cat_id 
    WHERE
        1 = 1 
        AND cats.access <= 3 
        AND 1 = 1 
        AND 1 = 1 
        AND cats.published = 1 
        AND (
            1 = 1
        ) 
        AND (
            1 = 1
        ) LIMIT 12

Related Articles



* original question posted on StackOverflow here.