-
Notifications
You must be signed in to change notification settings - Fork 1
count(*) vs exists()
tulpar008 edited this page Apr 13, 2015
·
1 revision
Date:2013-02-04
Title: count(*) vs exists()
Tags: Mysql
Category:It
I am still amazed at how many of the database applications written today still disregard some basic rules of thumb when it comes to accessing the data. One in particular is the use of COUNT(*) to check to see if there are any rows that match some criteria. ——Andrew Kelly
判断是否存在cat=”极客“
的Article
count = Article.objects.filter(cat="极客").count()
if count:
# balabala...
SQL: SELECT COUNT(*) FROM table_article WHERE cat="极客";
exist = Article.objects.filter(cat="极客").exists()
if exist:
# balabalabala....
SQL: EXISTS (SELECT * FROM table_article WHERE cat="极客")
查看
- 方法一:
Scan count 1,
logical reads 3,
physical reads 0,
read-ahead reads 0,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0
- 方法二 👍
Scan count 1,
logical reads 2,
physical reads 0,
read-ahead reads 0,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0.
- 方法一:
Scan count 1,
logical reads 11,
physical reads 0,
read-ahead reads 0,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0.
- 方法二 👍
Scan count 1,
logical reads 2,
physical reads 0,
read-ahead reads 0,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0.
- 方法一:
Scan count 1,
logical reads 1241,
physical reads 0,
read-ahead reads 331,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0.
- 方法二 👍
Scan count 1,
logical reads 5,
physical reads 0,
read-ahead reads 0,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0.