Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indexing int8 does not work, here is why and how! (must read)

Status
Not open for further replies.

PriceComparison

Programmer
Oct 24, 2004
6
0
0
US
I am a programmer for PriceComparison.com.

And I have just solved something quite remarkable and hard to solve. For anyone who has any index using an int8 / bigint field.
You should take time to read this.

The problem start when we noticed that any query with that int8 field is always slow. Finally we did an 'explain' of the query.
The server never even cared of using the index. It always does a sequential scan. No index scan eventhough the index is clearly there. We tried to reindex, recreate it, vacuum analyze, etc.... nothing works.

Then we accidentaly made it work?!? Strange?
Here is our first query (the one that does not work).

select * from product where item_number=1234

this query will NEVER use the index.

then we tried this:

select * from product where item_number='1234'

Notice the difference is just the single quotes!!! Now it uses the index all the time!

Wow, and this only happens with int8 / bigint.

I hope this will help somebody.

Andrew_@_PriceComparison.com
 
Yes, it is simply a matter of typecasting, and I'm glad it is solved in version 8.0. Another way to fix the queries for indexes on different int types is to explicitly cast the constant to the right type:
Code:
select * from product where item_number=1234::int8
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top