A
A
AlikDex2015-09-21 21:33:14
MySQL
AlikDex, 2015-09-21 21:33:14

Which of these two queries is the least resource intensive?

I torture DLE requests. You need to select all posts from 1 category.
conditionally, there are posts dle_post (posts still have a second plate, with numbers. i.e. total -4th dle_post_extras ):
post_id | title | ...
categories dle_category :
cat_id | title | ...
link through table dle_category_map :
post_id | category_id
Option 1:

EXPLAIN SELECT p.id, p.autor, p.date, p.short_story, CHAR_LENGTH(p.full_story) as full_story, p.xfields, p.title, p.category, p.alt_name, p.comm_num, p.allow_comm, p.fixed, e.news_read, e.allow_rate, e.rating, e.likes, e.dislikes, e.vote_num, e.votes, e.view_edit, e.editdate, e.editor, e.reason 
FROM `dle_post` AS p USE INDEX (cat_posts)
  INNER JOIN `dle_category_map` AS cm ON ( p.id = cm.news_id AND cm.cat_id=11) 
LEFT JOIN dle_post_extras e ON (e.news_id=p.id) 
WHERE approve=1 AND date < (NOW() + INTERVAL 0 MINUTE) 
ORDER BY fixed desc, date DESC LIMIT 1536,32

explain:
| id | select_type | table | type   | possible_keys | key       | key_len | ref                      | rows | Extra       |
|----|-------------|-------|--------|---------------|-----------|---------|--------------------------|------|-------------|
| 1  | SIMPLE      | p     | ref    | cat_posts     | cat_posts | 1       | const                    | 8887 | Using where |
| 1  | SIMPLE      | cm    | eq_ref | PRIMARY       | PRIMARY   | 8       | hdvideoscript.p.id,const | 1    | Using index |
| 1  | SIMPLE      | e     | ref    | news_id       | news_id   | 4       | hdvideoscript.p.id       | 1    | NULL        |

option 2:
EXPLAIN SELECT p.id, p.autor, p.date, p.short_story, CHAR_LENGTH(p.full_story) as full_story, p.xfields, p.title, p.category, p.alt_name, p.comm_num, p.allow_comm, p.fixed, e.news_read, e.allow_rate, e.rating, e.likes, e.dislikes, e.vote_num, e.votes, e.view_edit, e.editdate, e.editor, e.reason 
FROM `dle_post` AS p 
JOIN ( 
    SELECT t2.id 
    FROM `dle_post` AS t2 USE INDEX (cat_posts) 
    	    INNER JOIN `dle_category_map` AS cm ON ( cm.news_id = t2.id AND cm.cat_id=11) 
    WHERE t2.approve=1 AND date < (NOW() + INTERVAL 0 MINUTE) 
    ORDER BY fixed desc, date DESC LIMIT 1536,32 
  ) AS j USING (id) 
LEFT JOIN `dle_post_extras` e ON (p.id=e.news_id)

explain:
| id | select_type | table      | type   | possible_keys | key       | key_len | ref                       | rows | Extra       |
|----|-------------|------------|--------|---------------|-----------|---------|---------------------------|------|-------------|
| 1  | PRIMARY     | <derived2> | ALL    | NULL          | NULL      | NULL    | NULL                      | 1568 | NULL        |
| 1  | PRIMARY     | p          | eq_ref | PRIMARY       | PRIMARY   | 4       | j.id                      | 1    | NULL        |
| 1  | PRIMARY     | e          | ref    | news_id       | news_id   | 4       | j.id                      | 1    | NULL        |
| 2  | DERIVED     | t2         | ref    | cat_posts     | cat_posts | 1       | const                     | 8887 | Using where |
| 2  | DERIVED     | cm         | eq_ref | PRIMARY       | PRIMARY   | 8       | hdvideoscript.t2.id,const | 1    | Using index |

Both return with approximately the same time: But it varies very much from one category to another, and strongly depends on the current page (i.e. the further, the more time the request eats up.)
is ~ 100ms from the end, ~2ms at the beginning.
Maybe there is a third option? Or finish one of these to a sane state.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2015-09-21
@AlikDex

And isn't the dle_category_map of the table enough to select posts for the desired category and, based on them, make the necessary selection by links?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question