bitsCN.com
mysql> select sql_calc_found_rows * from tbl_name -> where id > 100 limit 10;
mysql> select found_rows();
使用SQL_CALC_FOUND_ROWS能够在查询时为您事先准备好符合where条件的记录数目,然后只要在随后执行一句select FOUND_ROWS(); 就能获得总记录数。
这个方法有一个副作用,当使用了SQL_CALC_FOUND_ROWS以后,将不能使用查询缓存、在特殊情况下反而或损失一些性能。
例如,一个文章表,所有文章做了主键ID,并做了CREATE_TIME DESC的索引。这样在执行
SELECT * FROM ARTICLE ORDER BY ID DESC LIMIT 10 或者 CREATE_TIME DESC LIMIT 20 时,数据库引擎可以完全根据索引返回最新文章而不会管你有多少符合的记录,但用了SQL_CALC_FOUND_ROWS后引擎不得不扫描全表以确定全部记录数。
但无论怎样,这个 SQL_CALC_FOUND_ROWS 非常适合where字句异常复杂耗时的情况。在频繁使用查询的应用中,这个方法能够带来1/3的数据库性能提升。
Never, never use SQL_CALC_FOUND_ROWS it’s just equally slow then “SELECT COUNT(*) FROM table”, but you have to do it on every page. The count(*) variant you can cache at last, so you don’t have to do it on every page.
(用SQL_CALC_FOUND_ROWS 每页每次查询都会做Count;用 count(*),则可以自己缓存结果)
And there’s even another way to avoid the count on paging. It’s the way Facebook does paging on some places. Facebook don’t give you the usually list of pages from 1 to n there, were you can click at any page. They just give you the page before, the current page and the next page, if there’s one. On the application side it’s very easy to find out if you have a page before the current, when you are on the second page there’s one before (so no surprise here). But what about the next page if you don’t want to make a count. Easy stuff, let me predict you display 10 items per page, so query 11 items instead of 10 per page. This one extra item will cost you nearly nothing and now you can count the returned rows in your application. If you have more than ten rows you have a next page and you can happily throw number eleven away.
(只显示上下页;程序很容易知道有没有上一页;判断下一页,只需要多查询1条,则可以判断有没有下一条记录了。)
seo特别版程序介绍:注意:普通用户建议使用淄博分类信息港程序普通版本。主要针对seo需要增加了自定义功能:自定义文件路径;自定义文件名;自定义关键字。这些功能的作用,只有自己体会了。以下是淄博分类信息港程序的介绍:淄博分类信息港程序一套现成的城市分类信息网站发布系统。发布管理房屋、人才、招租、招聘、求购、求租、搬迁、运输、二手交易、招生培训、婚介交友等各类信息的发布和查询。淄博分类信息港发布程序
0
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) LEFT JOIN wp_wti_like_post on wp_wti_like_post.post_id=wp_posts.ID WHERE 1=1 AND ( ( post_date_gmt > ’2013-11-08 15:37:48′ ) ) AND ( wp_term_relationships.term_taxonomy_id IN (5) ) AND wp_posts.post_type = ‘post’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’) GROUP BY wp_posts.ID ORDER BY wp_wti_like_post.value DESC,wp_posts.post_date DESC LIMIT 0, 2
这是一段wordpress程序生成的sql语句。。
(原文地址:http://tanteng.sinaapp.com/2013/11/sql_calc_found_rows/)
bitsCN.com
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号