boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 常用SQL的误区???--新手请绕行
相关主题
query running long time
其实我发现了CODE的写得好不好
奇怪的 SQL 问题
SQL Server Question: how delete works
怎样快速得到两个表的交集
包子请教query
MySQL DBA 的前途
有什么优化query的常用方法
面试回来发考题1
这个 query 为什么可以 update multiple rows
相关话题的讨论汇总
话题: count话题: join话题: inner话题: exists话题: exist
进入Database版参与讨论
1 (共1页)
B*****g
发帖数: 34098
1
大家讨论一下,下面哪个好,你在工作中一般用哪个?讨论请注明数据库和版本
1. inner join vs EXISTS vs IN
2. outer join vs NOT EXISTS vs NOT IN
3. count(*) vs count(column) vs count(1)
B*****g
发帖数: 34098
y****w
发帖数: 3747
3

inner join and [not] exists. in most situation the performance diff is slim
. IN only for short list.
not exists, then outer join. NOT IN only for short list.
count(1) and count(column) ---- they are different!

【在 B*****g 的大作中提到】
: 大家讨论一下,下面哪个好,你在工作中一般用哪个?讨论请注明数据库和版本
: 1. inner join vs EXISTS vs IN
: 2. outer join vs NOT EXISTS vs NOT IN
: 3. count(*) vs count(column) vs count(1)

B*****g
发帖数: 34098
4

另外count(column)应该是指count(pk)

slim

【在 y****w 的大作中提到】
:
: inner join and [not] exists. in most situation the performance diff is slim
: . IN only for short list.
: not exists, then outer join. NOT IN only for short list.
: count(1) and count(column) ---- they are different!

c******n
发帖数: 7263
5
想起以前和同事聊天说的一道专门用来恶心人的面试题:
oracle里面怎么判断一个表不是空的最快?

【在 B*****g 的大作中提到】
: 赞
: 另外count(column)应该是指count(pk)
:
: slim

w*****m
发帖数: 20421
6
select top 1 1 from table ?

【在 c******n 的大作中提到】
: 想起以前和同事聊天说的一道专门用来恶心人的面试题:
: oracle里面怎么判断一个表不是空的最快?

B*****g
发帖数: 34098
7
oracle

【在 w*****m 的大作中提到】
: select top 1 1 from table ?
B*****g
发帖数: 34098
8
是不是就是说不能用count所有的?
SELECT /*+ First_Rows(1) */ 1
FROM tab a
WHERE ROWNUM = 1
12c的top-N没用过

【在 c******n 的大作中提到】
: 想起以前和同事聊天说的一道专门用来恶心人的面试题:
: oracle里面怎么判断一个表不是空的最快?

c******n
发帖数: 7263
9
其实我也不是很确定正确答案是什么,不过我也认为这样应该是最快的,呵呵
我觉得面试的时候问这种问题很无聊的,和茴字四种写法一样,除非是看那个人非常不
顺眼了,换句话说,如果我问了这个问题,那个人就要被挂了,hehe

【在 B*****g 的大作中提到】
: 是不是就是说不能用count所有的?
: SELECT /*+ First_Rows(1) */ 1
: FROM tab a
: WHERE ROWNUM = 1
: 12c的top-N没用过

B*****g
发帖数: 34098
10
我觉得trick在于有些人会用select count(*) from tab,这样做要是table真没东西也
行,一旦table有东西而且巨大。。。

【在 c******n 的大作中提到】
: 其实我也不是很确定正确答案是什么,不过我也认为这样应该是最快的,呵呵
: 我觉得面试的时候问这种问题很无聊的,和茴字四种写法一样,除非是看那个人非常不
: 顺眼了,换句话说,如果我问了这个问题,那个人就要被挂了,hehe

相关主题
SQL Server Question: how delete works
怎样快速得到两个表的交集
包子请教query
MySQL DBA 的前途
进入Database版参与讨论
y*****g
发帖数: 677
11
In MySQL, for just row count,
count(1) is same as count(*) or count(pk)
I doubt there is any difference between count(*), count(pk), count(1) in
MySQL.
MySQL optimizer will try to use a shorter index to find row count.
y*****g
发帖数: 677
12
In MySQL,
usually inner join is better than IN(subquery).
if it is a finite number of list, although it can be a very long list, such
as in (1,2,3,...........,10000), it can still perform at least as well as
other alternatives.
s**********o
发帖数: 14359
13
SQL SERVER
如果就是SELECT,那就INNER JOIN WITN NOLOCK
如果是TRANSACTION,要先用IF EXIST,然后UPDATE/INSERT/DELETE
如果是复杂的JOIN + TRANSACTION TOUCH数据很多,一般是先弄个CTE把
要更新的KEY找出来,然后UPDATE/INSERT/DELETE
y*****g
发帖数: 677
14
gotcha, WITN --> WITH
o***i
发帖数: 603
15
這是釣魚吧?

【在 B*****g 的大作中提到】
: 大家讨论一下,下面哪个好,你在工作中一般用哪个?讨论请注明数据库和版本
: 1. inner join vs EXISTS vs IN
: 2. outer join vs NOT EXISTS vs NOT IN
: 3. count(*) vs count(column) vs count(1)

B*****g
发帖数: 34098
16
你最近跑哪去了

【在 o***i 的大作中提到】
: 這是釣魚吧?
d****n
发帖数: 12461
17
count(*)带null,count(1)不带null,然后呢?
u*********e
发帖数: 9616
18
我怎么觉得这几项列在一起很奇怪?
inner join一般不都是table或者是一个selected set 么?Exists, In 不是select
where 中定义的某一个条件么

【在 B*****g 的大作中提到】
: 大家讨论一下,下面哪个好,你在工作中一般用哪个?讨论请注明数据库和版本
: 1. inner join vs EXISTS vs IN
: 2. outer join vs NOT EXISTS vs NOT IN
: 3. count(*) vs count(column) vs count(1)

d****n
发帖数: 12461
19
应该是和optimizer有关的吧,什么时候inner join被优化成exist了之类的。

【在 u*********e 的大作中提到】
: 我怎么觉得这几项列在一起很奇怪?
: inner join一般不都是table或者是一个selected set 么?Exists, In 不是select
: where 中定义的某一个条件么

w*r
发帖数: 2421
20
INNER JOIN <> EXIST
inner join 允许Cartesian,
exist 不允许
exist要求 in set要sort取distinct ,sort的cost就imply的
inner join不要求sort, 但是通常都会做sort merge join
exist对于stack tables (oracle/db2/sqlserver)优化的比较好, 对于MPP RDBMS的优
化不友好,
exist上要考虑null的side effect 特别是not exist
如果exist 的inset本身是unique index, 其cost和inner join 一样
1 (共1页)
进入Database版参与讨论
相关主题
这个 query 为什么可以 update multiple rows
Re: negotiate弄砸offer了吗?求祝福! (转载)
同事被FIRE掉了
求思路:如何保存/更新CTE recursive query结果
SQL中NOT EXIST和NOT IN有什么区别?
再问not exist和not in
有人给大包子了
新手请教SQL 语法问题- alias 和 join
有没有人对MPP analytic database有兴趣?
求一本书:TeraData公司的数据模型建模的书
相关话题的讨论汇总
话题: count话题: join话题: inner话题: exists话题: exist