简体   繁体   English

WordPress - SQL 查询优化

[英]WordPress - SQL Query Optimization

Is it better to add the condition (meta_key = 'items') to my query or this is not necessary.将条件(meta_key = 'items') 添加到我的查询中是否更好,或者这不是必需的。

I told myself that by limiting the research to only if meta_key equal to items will optimize my SQL Query.我告诉自己,将研究限制在只有当 meta_key 等于 items 时才能优化我的 SQL 查询。

global $wpdb;
$value = 'my_vaue';     
$result = $wpdb->get_results("SELECT * FROM wp_usermeta WHERE (meta_key = 'items') AND (meta_value LIKE '%%$value%%')");

Thanks!谢谢!

This is your query:这是您的查询:

SELECT *
FROM wp_usermeta
WHERE meta_key = 'items' AND
      meta_value LIKE '%%$value%%';

(Why do you have two '%' in the pattern? That is equivalent to one.) (为什么模式中有两个'%' ?这相当于一个。)

Your question is not one of performance, but of functionality.您的问题不是性能问题,而是功能问题。 The alternative is:替代方案是:

SELECT *
FROM wp_usermeta
WHERE meta_value LIKE '%%$value%%';

The first searches through the "items" for your value.第一个在“项目”中搜索您的价值。 The second searches through all rows in the table.第二个搜索表中的所有行。 Use the one that best matches the functionality you want.使用与您想要的功能最匹配的那个。

Unless "items" are very rare in the table, both queries will do a full table scan.除非“项目”在表中非常罕见,否则两个查询都会进行全表扫描。 The performance will probably be dominated by reading the data.性能可能会以读取数据为主。 However, like is a bit expensive so the first version might be a wee bit faster.但是, like有点贵,所以第一个版本可能会快一点。

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

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