2

I have a site with 100k+ posts and I am trying to use this query:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 5

After searching for quite some time now, I came to understand that it's a pagination query. The query takes too much time and is not efficient.

Does anyone know of a solution for this problem?

P.S. I have already searched the old answers but they do not provide any solution.

jdyuen
  • 9
  • 1
Pavan Bhat
  • 21
  • 4
  • I would change this to `wp_posts.post_status IN( 'publish' ,'private')` but i don't know where this SQL came from. And it's more of a readability thing, because it will be optimized to this anyway.... I would also try it without the Sorting as that can kill performance. – ArtisticPhoenix Mar 15 '19 at 08:36
  • It executes by default so where the edit has to be happened..? in WP_Query.php file..? – Pavan Bhat Mar 15 '19 at 12:16

2 Answers2

0

Yes, SQL_CALC_FOUND_ROWS takes more time than two queries. Please check this link:

Which is fastest? SELECT SQL_CALC_FOUND_ROWS FROM table, or SELECT COUNT(*)

Lot of users or developers have confirmed, you can read comments of users.

Try to use SELECT COUNT(*) statement to find number of records.

Ishpreet
  • 659
  • 5
  • 19
0

In WordPress you can use WP_Query for getting the posts found

$obj_name = new WP_Query($args);
$num = $obj_name->post_count; 
mujuonly
  • 11,370
  • 5
  • 45
  • 75