一句SQL命令优化缩小你的wordpress数据库,手动清除Transients
Transients
是临时的瞬时的意思,wp暂时存储缓存数据在数据库中通过给它一个自定义的名称和时间之后,它将到期被删除。但有时,瞬变WP和无数的插件可以设定的数据库中大量的空间,使我们的wordpress数据库变得大而臃肿,查询速度变慢,使用一个简单的SQL查询,我们可以轻松删除他们,从而优化我们的WP数据库。One sentence of SQL command optimizes and shrinks your wordpress database. Manually clearing TransientsTransients means temporary and transient. WP temporarily stores cached data in the database by giving it a custom name and time, and it will be deleted after expiration. But sometimes, transient WP and countless plug-ins can set a large amount of space in the database, making our wordpress database become large and bloated, and the query speed becomes slow. Using a simple SQL query, we can easily delete them, thereby Optimize our WP database.
The following content comes from I love boiled fish. What is WordPress Transients API Transients means transient. WordPress Transients API is the easiest way for WordPress to cache some complex SQL queries and calculation results. It gives the data that needs to be cached an expiration time, and it will be automatically deleted when the time is up.
So if you need to store some data with a certain life cycle when making a WordPress plugin, the Transients API is the best choice.
Where the data cached by the WordPress Transients API is stored depends on your server settings. If Memcached is enabled on your server, the cached data will be stored in the memory of Memcached. If it is not enabled, it will be stored in the Options table of the WordPress database.
The function of the WordPress Transients API mentioned above says that when the server is not turned on, the data is stored in the Options table, so its interface function is basically the same as the WordPress Option API (get_option, add_option, update_option, delete_option)), the only difference is the Transients API There is an expiration time. So the WordPress Transients API has three similar functions: set_transient() // save a temporary data to the cache get_transient() // get a temporary data from the cache delete_transient() // delete a temporary data from the cache if you Use the function get_transient to get a temporary variable, it has expired or does not exist, then return false. In addition, the Transients API will not fill the Options table of the database, because once the temporary variable expires, it will be automatically deleted the next time it is retrieved.
以下内容来自 我爱水煮鱼
什么是 WordPress Transients API
Transients 是瞬时的意思,WordPress Transients API 是 WordPress 用来缓存一些复杂的 SQL 查询和运算结果的最简单的方法。它给这些需要缓存的数据一个过期时间,并且时间一到就会自动删除。
所以如果你在制作 WordPress 插件的时候,需要存储一些有一定生命周期的数据的时候,Transients API 是最好的选择。

WordPress Transients API 缓存的数据存储在哪里
这个取决你的服务器设置,如果你的服务器开启 Memcached,那么缓存的数据就存在 Memcached 的内存中,如果没有开启的话,则存储到 WordPress 数据库的 Options 表中。
WordPress Transients API 的函数
上面说到服务器没有开启的时候,数据是存储到 Options 表中的,所以它接口函数和 WordPress 的 Option API (get_option
, add_option
, update_option
, delete_option
))基本一样,唯一区别就是 Transients API 有一个过期时间。所以 WordPress Transients API 有类似的以下三个函数:
set_transient() // 保存一个临时数据到缓存中
get_transient() // 从缓存中获取一个临时数据
delete_transient() // 从缓存中删除一个临时数据
如果你使用函数 get_transient
去获取一个临时变量,它已经过期或者不存在,则返回 false。另外 Transients API 不会将数据库的 Options 表充满,因为临时变量一旦过期,下次获取的时候就会自动被删除。
WordPress Transients API 例子
假设你要获取博客的流量最高的 10 篇文章,这个要设计复杂的 SQL 查询,而流量最高的 10 篇文章一般来说在一段时间(比如:12小时)之内是不会变化的,所以我们可以把这个数据通过 Transients API 先缓存了。代码如下:
function wpjam_get_top_10_posts(){
$top_10_posts = get_transient('top_10_posts');
if(false === $top_10_posts){ // 临时变量过期了或者根本就没有创建
// 通过 SQL 查询获取流量最高的 10 篇文章,
$top_10_posts = get_most_viewed(10);
// 把临时变量存到数据库中,时间为 12 个小时
set_transient('top_10_posts', $top_10_posts, 60*60*12);
}
return$top_10_posts;
}
其中 get_most_viewed
是它是用来获取流量最高的文章的函数。
如果由于某种原因某篇流行文章删除,或者新的文章发布了,这个时候可能流量最高的文章都可能发生变化,我们需要使用 delete_transient
函数把这个临时变量删除了。代码如下:
add_action('publish_post', 'wp_top_10_posts_delete', 0);
add_action('delete_post', 'wp_top_10_posts_delete', 0);
functionwp_top_10_posts_delete(){
delete_transient('top_10_posts');
}
首先,登录到你的phpmyadmin
和选择你的WordPress
数据库。一旦完成,单击sql
按钮打开sql
命令窗口。
然后,只需粘贴下面的sql命令并执行它。
DELETE FROM `wp_options` WHERE `option_name` LIKE ('%\_transient\_%');