admin管理员组文章数量:1122826
My plugin will handle large amounts of data that could could result in the queries reading over 5000 items in a database query. The admin will provide a set of CSV files that get stored/organized in the database. The user will have some common results when they go to specific pages. I can predict what that query is in most cases. I wanted to store the array of results in cache so that query is faster.
I looked into wp_cache_get, wp_cache_set, wp_cahce_delete, etc
but it seems this may not be the best option since it's not persistent. The codex says I would need an extra plugin to handle persistent caching but I'd rather not require ANOTHER plugin. I could store the array serialized in the database but not sure if that's the best route for efficiency or even an acceptable workaround to the cache issue.
My plugin will handle large amounts of data that could could result in the queries reading over 5000 items in a database query. The admin will provide a set of CSV files that get stored/organized in the database. The user will have some common results when they go to specific pages. I can predict what that query is in most cases. I wanted to store the array of results in cache so that query is faster.
I looked into wp_cache_get, wp_cache_set, wp_cahce_delete, etc
but it seems this may not be the best option since it's not persistent. The codex says I would need an extra plugin to handle persistent caching but I'd rather not require ANOTHER plugin. I could store the array serialized in the database but not sure if that's the best route for efficiency or even an acceptable workaround to the cache issue.
- 2 You could use custom tables with indexes optimized for your queries. Have you tried that? – fuxia ♦ Commented Feb 13, 2016 at 5:34
- Storing the post-ids in transient variables could help, but as you've mentioned it might not be the most efficient. 5000 results aren't that many though & using transients may be a more economical alternative to custom tables. Reddis might do the trick too. You could also maybe bypass the database and just work from the csv data. – admcfajn Commented Dec 3, 2021 at 5:26
- with an object cache installed those functions are persistent! Even without one though they give a performance boost by avoiding duplicate queries during the same request – Tom J Nowell ♦ Commented Jan 4, 2024 at 14:46
2 Answers
Reset to default 0Caching is Your Friend
I understand your frustration with adding another plugin, but a method for extending WordPress cache is highly recommended for any WordPress sites getting any kind of traffic. These plugins allow WordPress to store cache somewhere better suited for storing and quickly accessing this data, such as Memcache, Redis, or even in the local file structure.
- W3 Total Cache offers the widest variety of destinations for cache.
- WP Super Cache offers very quick access to file cache.
- Batcache offers direct access to Memcache.
- Redis Cache offers direct access to Redis cache.
At the end of the day, it all depends on what's available on your server.
In my opinion, wp_cache
is the way to go for what you need. It will allow you to store these values for quick access later using a much faster technology than storing them in the database.
My plugin will handle large amounts of data that could could result in the queries reading over 5000 items in a database query.
If your rows are small and your tables are indexed correctly this may not be an issue.
Until you've actually measured this and found a problem though I think this is premature.
I looked into
wp_cache_get
,wp_cache_set
,wp_cache_delete
, etc but it seems this may not be the best option since it's not persistent.
With an object cache dropin and the right software it can and will be persistent. This is usually the case on proper enterprise hosting or if you run your own server, occasionally on managed hosts via Redis/Memcached/APCu.
Caching plugins can also do this though you should measure performance as sometimes file based caches are slower not faster.
But even if it is not persistent you should still consider them as a means of avoiding duplicate queries. Calling get_post_meta
10x in a row with the same arguments won't create 10 queries because it's using WP_Cache
behind the scenes via those functions.
And if in doubt, there are always transients that store temporary/expiring data in options.
本文标签: databaseImproving wpdb queries with large data
版权声明:本文标题:database - Improving wpdb queries with large data 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282850a1926775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论