0

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.

1 Answer 1

0

This is the query:

SELECT DISTINCT cats.name AS cat_name, cats.alias AS cat_alias, content.catid AS cat_id,
       content.title, content.introtext AS text, content.created AS date,
       content.publish_up AS date_publish, content.id, content.alias, content.hits, content.plugins 
FROM t0nbl_k2_items content JOIN
     t0nbl_k2_categories 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;

One big performance hog is distinct. If you can remove it, remove it. Next, the left join is being undone by the where clause. So, you might as well express it as an inner join.

I would suggest indexes on t0nbl_k2_items(trash, published, access, publish_up, publish_down, catid) and on t0nbl_k2_categories(catid, access). With the distinct there is no way to eliminate the final order by. Even without the distinct, I'm not sure if there is a way.

2
  • thanks for your answer. yes by removing destinct it goes down to 30ms. but i don't know when there will be duplicated rows? Oct 31, 2014 at 11:57
  • You can check the individual tables to see if the join produces unwanted duplicates. There is a good chance that it will not produce duplicates unless you have duplicates in one of the tables -- which would be a data quality problem. Oct 31, 2014 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.