November 29, 2009 at 5:26 pm
Hi, I have a table that have a index, a field may be null.
When I am doing a query with this index, it doesn't used, because I cast the null field.
e.g:
Persons
identification varchar(20) NOT NULL,
name varchar(30) NULL,
address varchar(100) NULL
telephone varchar(20) NULL
Unique Index
identification, name
Query
select *
from Persons with (nolock)
where identification = '52801333'
and isnull(name, '') = ''
How can I write the query and use my index?
Thanks.....
November 29, 2009 at 5:33 pm
to take advantage of an index, you can't use any functions on the column in the WHERE statement; the isnull(name, '') = '' doesnt allow a Sargable search, so a table scan has to be used.
i think this will use the index with an index scan; still a scan, bet better than a table scan.
select *
from Persons with
where identification = '52801333'
and name IS NULL OR name= ''
Lowell
November 30, 2009 at 12:42 am
thanks, i have the same problem!
Công ty Đất Quảng - Cong ty Dat Quang - http://datquangda.com.vn
Bất động sản, mua bán nhà đất khu đô thị mới Nam An Khánh Hà Nội.
Trụ sở: Phòng 303 nhà N6E – Trung Hoà Nhân Chính – Thanh Xuân – Hà Nội
Văn phòng giao dịch: Số nhà 10, ngách 23, ngõ 61, Trần Duy Hưng, Trung Hòa, Cầu Giấy, Hà Nội
Điện thoai: 04.3556 2516 - 3556 2552 - 2247 5733
Fax: 04.3556 2566 - 3556 2140
Email:datquangda@gmail.com
Website: http://www.datquangda.com.vn
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply