SQL where field in vs. where field = with multiple ors? [英] SQL where field in vs. where field = with multiple ors?

查看:27
本文介绍了SQL where field in vs. where field = with multiple ors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就性能而言,使用哪个更好?...关于可读性/可理解性?...关于公认的标准?

Which of these is better to use in regard to performance? ...in regard to readability / understandability? ...in regard to accepted standards?

SELECT *
FROM Wherever
WHERE Greeting IN ('hello', 'hi', 'hey')

SELECT *
FROM Wherever
WHERE Greeting = 'hello' 
   OR Greeting = 'hi'
   OR Greeting = 'hey'

第一个对我来说似乎更直观/清晰,但我不确定可接受的标准和性能.

The first seems more intuitive / clear to me, but I'm unsure of accepted standards and performance.

推荐答案

它更具可读性,并且更被普遍接受:

It more readable, and more universally accepted to do:

SELECT *
FROM Wherever
WHERE Greeting in ('hello', 'hi', 'hey')

所有现代 SQL 服务器都会优化您的查询,因此它们都可能更改为在服务器上运行的相同代码,因此性能差异可以忽略不计或不存在.

All modern SQL servers optimize your queries, so they're both likely to be changed into the same code that runs on the server, so performance differences will be negligible or non-existent.

显然 in 选项更快,因为它计算二进制查找,而多个 = 只是单独计算每个语句.

Apparently the in option is faster, as it evaluates to a binary lookup, whereas the multiple = just evaulates each statement individually.

这篇关于SQL where field in vs. where field = with multiple ors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆