如果我的查询中有太多OR运算符,如何优化mysql查询? [英] how to optimize mysql query if i have too many OR operators in my query?
问题描述
使用PHP和MySQL
我从facebook上获取了一系列的facebook用户ID.
现在,我想在我的应用程序中为此数组找到相应的用户名.
很明显,在我的应用程序中,用户表包含唯一的用户名和唯一的fb_uid值.
我对编程的基本了解使我想到了两种方式:
1)使用循环,遍历fb_uid数组并找到用户名.
OR
2)创建一个怪物查询,例如从用户中选择fb_uid = value1或fb_uid = value2 ...
那么还有更好的出路吗?
谢谢.
请改用SQL的IN
运算符:
select distinct(username) from users where fb_uid in (value1, value2, ...)
http://www.w3schools.com/SQl/sql_in.asp
using PHP and MySQL
i have grabbed an array of facebook user ids from facebook.
Now i want to find the corresponding username in my application for this array.
Clearly in my application the user table contains unique username and unique fb_uid values.
my rudimentary understanding oof programming led me to 2 ways:
1) use a loop and run through the array of fb_uid and find the username one by one.
OR
2) create a monster query like select distinct(username) from users where fb_uid = value1 OR fb_uid = value2 ...
so is there a better way out?
Thank you.
Use SQL's IN
operator instead:
select distinct(username) from users where fb_uid in (value1, value2, ...)
http://www.w3schools.com/SQl/sql_in.asp
这篇关于如果我的查询中有太多OR运算符,如何优化mysql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!