简体   繁体   English

Wordpress一直在执行这个SQL查询?

[英]Wordpress is executing this SQL query all the time?

My Wordpress site doesn't have a lot of traffic but I keep seeing this SQL query via "SHOW PROCESS;" 我的Wordpress网站没有很多流量,但我通过“SHOW PROCESS”继续看到这个SQL查询 in MySQL: 在MySQL中:

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')
ORDER BY wp_posts.post_date DESC LIMIT 0, 10

I have turned off all plugins and updated to the latest Wordpress version (4.1). 我已关闭所有插件并更新到最新的Wordpress版本(4.1)。 And I also use the default theme built in with WordPress (2014). 我还使用WordPress(2014)内置的默认主题。

Whenever I run "SHOW PROCESS;", I can see at least 3-8 of this query running, sometimes even more, like 10+ of this query while the traffic is quite light (even idle). 每当我运行“SHOW PROCESS;”时,我都可以看到至少有3-8个查询正在运行,有时甚至更多,比如此查询的10+,而流量非常轻(甚至空闲)。

I am asking because MySQL is using a lot of CPU resource and I am wondering why this query keeps showing up multiple times every second I run "SHOW PROCESS" on. 我问,因为MySQL正在使用大量的CPU资源,我想知道为什么这个查询每秒都会多次显示我运行“SHOW PROCESS”。

What you are seeing are normal and expected and there is not much you can do about that. 你所看到的是正常的和预期的,你无能为力。 It is normal to have up to 30 SQL queries on a page load. 在页面加载上最多有30个SQL查询是正常的。 More than that should be inspected. 应该检查更多。

So what are all these queries and where do they come from? 那么所有这些查询是什么?它们来自哪里?

  • On every page load, the main query is executed, even on a 404 page or when a bunch of garbage is enetered into the URL field/address bar. 每个页面加载时,即使在404页面上,或者当一堆垃圾进入URL字段/地址栏时,也会执行主查询。 The URL is parsed and converted in query arguments which is used by WP_Query to retrieve the posts from the db according to the arguments coming from the URL. URL在查询参数中被解析和转换, WP_Query使用它根据来自URL的参数从db中检索帖子。 WP_Query converts all the arguments and requests into a single SQL query which it uses to communicate with the db to retrieve what is been asked of it. WP_Query将所有参数和请求转换为单个SQL查询,它用于与数据库通信以检索对其的询问。 This is one db hit which you can't do anything about and most probabaly this is the SQL query from your question. 这是一个db命中,你无法做任何事情,大多数可能这是你的问题中的SQL查询。

  • Any custom query ( new WP_Query , get_posts and get_pages ) is converted to an SQL query to query and retrieve posts from the db 任何自定义查询( new WP_Queryget_postsget_pages )都将转换为SQL查询,以便从数据库中查询和检索帖子

  • Your navigation menu make use of functions that is converted to SQL queries to construct the main navigation menu 导航菜单使用转换为SQL查询的函数来构造主导航菜单

  • Function used to display tags, term and categories attached to a post uses SQL queries to retrieve the tags/terms/categories from the DB 用于显示附加到帖子的标签,术语和类别的函数使用SQL查询从DB检索标签/术语/类别

  • Most of the default widgtes uses custom queries and/or functions that uses SQL queries to get info from the db 大多数默认widgtes使用自定义查询和/或使用SQL查询从db获取信息的函数

  • Some of the conditional tags uses SQL queries to check a condition against an entry in the db 某些条件标记使用SQL查询来检查数据库中条目的条件

  • On pages, the main query makes an additional SQL query to retrieve the page template used for the specific page 在页面上,主查询进行额外的SQL查询以检索用于特定页面的页面模板

  • Wordpress runs some processes which uses SQL queries in the background Wordpress运行一些在后台使用SQL查询的进程

  • Related or popular post sections also uses custom queries which translates in SQL queries to retrieve the relevant posts 相关或热门的帖子部分还使用自定义查询,这些查询在SQL查询中进行转换以检索相关帖子

These are just some of the stuff that automatically hits the db on each page load, some can be controlled, others cannot, but in the end of the day, without these queries you won't have the visual experience or functionalities in your pages 这些只是在每个页面加载时自动命中数据库的一些内容,有些可以控制,有些则无法控制,但在一天结束时,如果没有这些查询,您将无法在页面中获得视觉体验或功能

I would suggest that you download and install Query Monitor in your test installation. 我建议您在测试安装中下载并安装Query Monitor Do not run this tool on production site as the info is printed to screen directly. 不要在生产站点上运行此工具,因为信息会直接打印到屏幕上。 This tool prints out all the queries/db hits on every page load and the time that these queries takes to execute. 此工具打印出每个页面加载的所有查询/ db命中以及执行这些查询所需的时间。 With this you will get a good idea of what is going on and which functions hits the db. 有了这个,你就可以很好地了解发生了什么以及哪些功能击中了数据库。

As I already stated, it is normal to have up to 30 SQL queries (db hits) on a single page load. 正如我已经说过的,在单个页面加载上最多有30个SQL查询(db命中)是正常的。 More that this should be inspected and this can normally be rectified by optimizing your code 更多应该检查这一点,这通常可以通过优化您的代码来纠正

The most important factor to remember and to consider is the amount of queries vs the actual time taken to execute them. 要记住和考虑的最重要因素是查询量与执行它们所花费的实际时间 This should be your most important factor in deciding which route to go. 这应该是决定走哪条路线的最重要因素。 I have shown many times in the past that four well constructed queries is sometimes 10 times faster than one query in which you want to do everything at once. 我过去曾多次表明,四个构造良好的查询有时比一次查询所需要的一个查询快10倍。

Also, make proper use of caches and transients on big bulky operations that cannot be streamlined. 此外,在无法简化的大型操作上正确使用缓存和瞬态。 You will at some stage run into a query that makes 70 db hits no matter what you try. 在某些阶段,无论您尝试什么,您都会遇到70分贝命中的查询。

EDIT 编辑

Just to add, the amount of traffic does not increase or decrease the amount of SQL queries that runs by default on a given page. 只是要添加,流量不会增加或减少默认情况下在给定页面上运行的SQL查询量。 The amount of SQL queries are solely determined by the processes being run to completely load the page SQL查询的数量完全取决于为完全加载页面而运行的进程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM