I have a simple query and table, and I would like to know which indexing is efficient on this kind of table and query.
In my table I have 3 columns
CREATE TABLE mYTable(ipFrom BIGINT, ipto BIGINT, url NVARCHAR(255))
and I am running this simple query.
SELECT url
FROM MyTable
WHERE ipto <= somevalue AND ipfrom >= somevalue
I have also created indexes on all 3 columns clustered on ipFrom and non-clustered on rest of 2. But this query giving me really poor performance in terms of CPU & reads.
Any suggestions.
What i have implemented is i like to redirect user based on the IP address. I have stored several IP ranges from different areas and states and redirect users on the basis of their IP to appropriate URL.
Yes i think i inserted the data in wrong manner in both columns(and will do column rename later), but point here is to minimize the CPU.
When i look into the execution plan there it converts data in where clause, i dont know why it is converting data in where clause. There is something like this
|--Clustered Index Seek(OBJECT:([T].[TC]), SEEK:([T].[C] > Convert([@V])
ipto >= somevalue AND ipFrom <= somevalue– Martin Smith Mar 13 '12 at 07:46somevalue BETWEEN ipfrom and ipto(as per @MartinSmith orsomevalue NOT BETWEEN ipfrom and ipto(as per your question)? – gbn Mar 13 '12 at 09:12@Vtobigintfromintprobably but that isn't the source of your problem. The problem is that the seek is only bounded on one end and so on average it will scan half the index testing every row meeting the>@Vcriteria to see if it meets the<criteria (presumably most of them won't). Jack's solution should work for you in minimising the number of rows scanned. – Martin Smith Mar 13 '12 at 10:54