MySQL 我可以删除 WordPress 安装的 wp_options 表中的瞬态吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10422574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I remove transients in the wp_options table of my WordPress install?
提问by Ryan
I have recently noticed that my wp_optionstable seems to be a bit large. It contains 1161 rows, and is about 2.1mb in size.
我最近注意到我的wp_options桌子似乎有点大。它包含 1161 行,大小约为 2.1mb。
I have installed Clean Options. It looks like development stopped on the plugin back in 2010, but it still did the job.
我已经安装了Clean Options。看起来插件的开发在 2010 年就停止了,但它仍然完成了这项工作。
I now have a long list of potentially orphaned entries. Is there an easy way to go about sorting these, and figuring out which to remove and which to keep? Also, could this be responsible for causing performance issues with the website?
我现在有一长串潜在的孤立条目。有没有一种简单的方法来对这些进行排序,并确定哪些要删除,哪些要保留?另外,这是否会导致网站出现性能问题?
Thank you for reading, any ideas are welcomed!
感谢您的阅读,欢迎任何想法!
Update: The Clean Options plugin returned some transients in the list, which lead me to find out that there are several hundred transient files in the wp_optionstable. There are a whole bunch that look like:
更新:Clean Options 插件在列表中返回了一些瞬态文件,这让我发现表中有数百个瞬态文件wp_options。有一大堆看起来像:
_site_transient_browser_5728a0f1503de54634b3716638..._site_transient_timeout_browser_03df11ec4fda7630a5..._transient_feed_83dcaee0f69f63186d51bf9a4..._transient_plugin_slugs_transient_timeout_feed_83dcaee0f69f63186d51bf9a4b...
_site_transient_browser_5728a0f1503de54634b3716638..._site_transient_timeout_browser_03df11ec4fda7630a5..._transient_feed_83dcaee0f69f63186d51bf9a4..._transient_plugin_slugs_transient_timeout_feed_83dcaee0f69f63186d51bf9a4b...
and so on. Like I said, there are several hundred rows that take look like this. Is it safe to just dump them?
等等。就像我说的,有几百行看起来像这样。直接扔掉它们安全吗?
Thanks
谢谢
回答by Lane
You can safetly dump them. Wordpress and some plugins will re-create transients as needed. A transient is more or less the stored value from a complex query. The results are saved as a transient so that the system doesn't have to perform a common query over and over, instead it just looks for the transient if it exists and hasn't expired. Of course, make a backup of your database before making a change lest something goes wrong!
您可以安全地丢弃它们。Wordpress 和一些插件会根据需要重新创建瞬态。瞬态或多或少是来自复杂查询的存储值。结果被保存为一个瞬态,这样系统就不必一遍又一遍地执行一个常见的查询,相反,它只是寻找瞬态,如果它存在并且没有过期。当然,在进行更改之前备份您的数据库,以免出现问题!
After backing everything up, you can run a mysql statement like this:
备份完所有内容后,您可以像这样运行 mysql 语句:
DELETE FROM `wp_options` WHERE `option_name` LIKE ('%\_transient\_%')
[EDIT:statement fixed with escape characters, after comment suggestion]
[编辑:在评论建议后用转义字符修复的语句]
回答by ClearCrescendo
You can delete transients as they will be recreated. There can be buildups of expired transients due to failure situations or design issues with some plugins. One way of coping with this is to remove expired transients while allowing current ones to perform their function. Purging only transients which are expired for a few days gives you a chance to monitor which plugins are resulting in stale transients, and take any action to fix issues or report issues.
您可以删除瞬变,因为它们将被重新创建。由于某些插件的故障情况或设计问题,可能会累积过期瞬变。解决此问题的一种方法是去除过期的瞬变,同时允许当前的瞬变执行其功能。仅清除过期几天的瞬变使您有机会监控导致过时瞬变的插件,并采取任何措施来修复问题或报告问题。
The following will find any wp*option tables in the database and delete the five largest transient options which are more than a week stale. This gives long enough for any plugin to delete options which they are going to purge themselves.
下面将查找数据库中的任何 wp*option 表并删除五个最大的超过一周的临时选项。这为任何插件提供了足够长的时间来删除它们将自行清除的选项。
#!/bin/bash
DBNAME="mydatabase"
DBUSER="${USER}"
DBPASSWD="secret"
MYSQLBIN=/usr/bin/mysql # OR MYSQLBIN=/usr/local/mysql/bin/mysql
MYSQL="${MYSQLBIN} -s -D ${DBNAME} -u ${DBUSER} -p${DBPASSWD}"
TMP=/var/tmp/
ENTRIES_FILE="${TMP}entries.$$"
# Find option tables
for OPTION_TABLE in $( echo 'show tables like "%wp%options";' | ${MYSQL} )
do
# Find up to five large long expired transients
${MYSQL} > ${ENTRIES_FILE} <<EOF
select option_name from ${OPTION_TABLE} where option_name in
(select concat("_transient",substr(option_name,19))
FROM ${OPTION_TABLE} WHERE option_name LIKE '_transient_timeout%' AND
option_value < UTC_TIMESTAMP() - INTERVAL 1 WEEK order by option_value)
order by length(option_value) desc limit 5;
EOF
for OPTION in $( < ${ENTRIES_FILE} )
do
echo Deleting ${OPTION} from ${OPTION_TABLE}
echo delete from ${OPTION_TABLE} where option_name = \"${OPTION}\"\; | ${MYSQL}
if [[ $? -eq 0 ]]; then
echo delete from ${OPTION_TABLE} where option_name = \"_transient_timeout${OPTION:10}\"\; | ${MYSQL}
fi
done
done
rm -f ${ENTRIES_FILE}
回答by Jonas Berlin
Install the plugin Delete Expired Transientsto automatically clean up the database on a daily basis.
安装插件Delete Expired Transients以每天自动清理数据库。

