开发者

Mysql, need some advice about the join and indexes on my query, also have a round(count(field) / 20 +1)

I'm trying to improve my query, I'm doing a count of all the records which are in a specific category, I'm also doing a sum to count how many pages each category will need to show all of the records in that category.

I'm looking to improve the speed of the query. I'm not 100% sure if the join and indexes are the most efficient.

I've tried the LIMIT at 5 and it makes no difference.

Heres my query

SELECT PageBase, round((count(pads.padid) / 20) + 1) as totpages FROM 
categories, pads WHERE pads.catid = categories.catid AND 
`RemoveMeDate` = '2001-01-01 00:00:00' group by categories.catid LIMIT 50,50

Heres the explain

Mysql, need some advice about the join and indexes on my query, also have a round(count(field) / 20 +1)

Heres the structure of the categories table

CREATE TABLE categories (
  CatID int(11) NOT NULL DEFAULT '0',
  NewASP text NOT NULL,
  `Specific` text NOT NULL,
  PageBase varchar(50) NOT NULL,
 PRIMAR开发者_高级运维Y KEY (CatID),
 KEY PageBase (PageBase)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Heres the query running

Mysql, need some advice about the join and indexes on my query, also have a round(count(field) / 20 +1)


Do any of these work any better? Please show EXPLAIN output.

This turns it into a subquery, which hopefully runs after the limit clause. EDIT: it doesn't seem to.

SELECT PageBase, 
  floor((ifnull((select count(*) from pads
   where pads.catid = categories.catid
     AND RemoveMeDate = '2001-01-01'),0)+19)/20) as totpages
FROM categories
LIMIT 50,50

This forces it to complete the 50,50, which hopefully takes no time at all, then performs 50 count subqueries.

SELECT PageBase,
   floor((ifnull((
        select count(1) from pads
        where pads.catid = C.catid
          AND RemoveMeDate = '2001-01-01'),0)+19)/20) as totpages
FROM (
    SELECT PageBase, CatID
    FROM categories
    LIMIT 50,50) C

FYI, count(1) and count(*) are the same. count(pads.catid) may be a tad slower, because it will need to work out if pads.catid is null before counting (in this query we know it cannot be null).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜