954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SQL_FOUND_ROWS() Makes query slow

Hi everyone,

select sql_calc_found_rows DIST_NO from acquired  WHERE FOLIO_NO='l01917' And STAT_FLAG <> 'Matched' union select  DIST_NO from transfrd   WHERE FOLIO_NO='l01917' And STAT_FLAG <> 'Matched'  LIMIT 0, 5;

The above query takes more than 4.5secs

if i run the query without sql_calc_found_rows the execution time is 0.7 secs

select DIST_NO from acquired   WHERE FOLIO_NO='l01917' And STAT_FLAG <> 'Matched' union select  DIST_NO from transfrd   WHERE FOLIO_NO='l01917' And STAT_FLAG <> 'Matched'  LIMIT 0, 5;


the total records found in both acquired and transfrd table is more than 25 lakhs how to overcome this issue and one more thing is here i indexed both FOLIO_NO and STAT_FLAG in both the tables

kartisathis
Newbie Poster
22 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Show us the result of
EXPLAIN
for both cases.
Make sure that you have a query cache set up. If that does not help, you cannot speed up the query, but you can cache the results of SQL_CALC_FOUND_ROWS in a variable and display it until major changes happen to your data.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

this is the result while am explain the query

+----+--------------+--------------+------+--------------------+----------+---------+-------+--------+-------------+
| id | select_type  | table        | type | possible_keys      | key      | key_len | ref   | rows   | Extra       |
+----+--------------+--------------+------+--------------------+----------+---------+-------+--------+-------------+
|  1 | PRIMARY      | acquired_tbl | ref  | FOLIO_NO,STAT_FLAG | FOLIO_NO | 103     | const | 136813 | Using where |
|  2 | UNION        | transfrd_tbl | ref  | FOLIO_NO,STAT_FLAG | FOLIO_NO | 103     | const | 880229 | Using where |
| NULL | UNION RESULT | <union1,2>   | ALL  | NULL               | NULL     | NULL    | NULL  |   NULL |             |
+----+--------------+--------------+------+--------------------+----------+---------+-------+--------+-------------+
kartisathis
Newbie Poster
22 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You