473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

explicit casting required for index use

Here's the basic issue: PostgreSQL doesn't use indexes unless a query
criterion is of exactly the same type as the index type. This occurs
even when a cast would enable the use of an index and greatly improve
performance. I understand that casting is needed to use an index and
will therefore affect performance -- the part I don't understand is why
postgresql doesn't automatically cast query arguments to the column
type, thereby enabling indexes on that column.
I have a table that looks like this (extra cols, indexes, and fk
constraints removed):

unison@csb=# \d paprospect2
Table "unison.paprosp ect2"
Column | Type | Modifiers
-------------+---------+-------------------------------------------------------------------
pseq_id | integer | not null
run_id | integer | not null
pmodel_id | integer | not null
svm | real |
Indexes: paprospect2_sea rch1 btree (pmodel_id, run_id, svm),

I often search for pseq_ids based on all of pmodel_id, run_id, and svm
threshold as below, hence the multi-column index.

Without an explicit cast of the svm criterion:

unison@csb=# explain select pseq_id from paprospect2 where pmodel_id=8210 and run_id=1 and svm>=11;
Index Scan using paprospect2_sea rch2 on paprospect2 (cost=0.00..432 68.93 rows=2 width=4)
Index Cond: ((pmodel_id = 8210) AND (run_id = 1))
Filter: (svm >= 11::double precision)

And with an explicit cast to real (the same as the column type and
indexed type):

unison@csb=# explain select pseq_id from paprospect2 where pmodel_id=8210 and run_id=1 and svm>=11::real;
Index Scan using paprospect2_sea rch1 on paprospect2 (cost=0.00..6.3 4 rows=2 width=4)
Index Cond: ((pmodel_id = 8210) AND (run_id = 1) AND (svm >= 11::real))
Note two things above: 1) The explicit cast greatly reduces the
predicted (and actual) cost. 2) The uncasted query eventually casts svm
to double precision, which seems odd since the column itself is real
(that is, it eventually does cast, but to the "wrong" type).

For small queries (returning ~10 rows), this is worth 100x in speed (9ms
v. 990ms... in absolute terms, no big deal). For larger result sets
(~200 rows), I've seen more like 1000x speed increases by using an
explicit cast. For the larger queries, this can mean seconds versus many
minutes.

Having to explicitly cast criterion is very non-intuitive. Moreover, it
seems quite straightforward that PostgreSQL might incorporate casts (and
perhaps even function calls like upper() for functional indexes) into
its query strategy optimization. (I suppose functional indexes would
apply only to immutable fx only, but that's fine.)

Thanks,
Reece

--
Reece Hart, Ph.D. rk*@gene.com, http://www.gene.com/
Genentech, Inc. 650/225-6133 (voice), -5389 (fax)
Bioinformatics and Protein Engineering
1 DNA Way, MS-93 http://www.in-machina.com/~reece/
South San Francisco, CA 94080-4990 re***@in-machina.com, GPG: 0x25EC91A0

Nov 12 '05 #1
0 1789

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
23198
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
11
503
by: Steven Bartley | last post by:
Error Message on execution: An unhandled exception of type 'System.InvalidCastException' occurred in CastObjectToDecimal.exe Additional information: Specified cast is not valid. The code in question is: object x = 15; decimal d = 0;
2
2898
by: rajivpopat | last post by:
I've been reading a discussion thread at http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/119f8362a9f5ff52 regarding typecasting generic collections to classical collections and vice-a-versa I faced a similar problem and solved it slightly differently... The apporach i seems to work but i am sure someone has a better apporach for solving this problem. In my case i was using nHibernate which was...
2
2657
by: John Richardson | last post by:
How does a custom casting operator work? I have a class that defines one, but doesn't work as expected (or perhaps, the way I want it to). I have the following example classes: ------------------ public class One : IOne { .... }//One public class Two {
8
338
by: mfc | last post by:
Suppose I have a Cookie class and a Factory Class. There are many types of Cookies and maybe one or more Factories with a ProcessCookie Method. Suppose all the PutInBox method does is decide what colour box to put the cookie into. But since the colour of the box depends on whatever Factory is boxing them (Different branding etc) the PutInBox method can't be in the Cookie class and has to be in the Factory class. The problem is pretty soon...
4
2157
by: Xavier Roche | last post by:
Hi folks, I have a probably rather silly question: is casting a char array in a char* a potential source of aliasing bug ? Example: a fonction returning a buffer taken in a circular buffer typedef struct foo_t foo_t; struct foo_t { int index;
1
2760
by: recherche | last post by:
Hola! I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain. Please help! Code Snippet 1:
17
2227
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
9
3464
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.