Thread: Using index name in select Query
Hi
I have table student with index built upon it naming index1 ,index2 so on and so forth.
I want to write a query by which i can force the optimizer to use the specified index.
Somewhat like :
select * from student use index(index1).
Is there any way to achieve this
Regards
Naman
I'm not sure what you mean by using a particular index.
If you want the index to be used to order the records, just add and ORDER BY line to the query:
SELECT * FROM student
ORDER BY index1.
On Saturday, October 19, 2013 8:24 AM, Naman <naman.bbps@gmail.com> wrote:
Hi
I have table student with index built upon it naming index1 ,index2 so on and so forth.
I want to write a query by which i can force the optimizer to use the specified index.
Somewhat like :
select * from student use index(index1).
Is there any way to achieve this
Regards
Naman
Naman wrote > I want to write a query by which i can force the optimizer to use the > specified index. No. Why? David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Using-index-name-in-select-Query-tp5775138p5775140.html Sent from the PostgreSQL - novice mailing list archive at Nabble.com.
Actually as part of project i have to achieve following task : suppose i have a table t1(a,b,c,d) I make an index as : CREATE INDEX index_abcd on t1(a) WHERE a is NOT NULL and b IS NULL and c IS NULL and d IS NOT NULL. There is a high probability but not 100% that optimizer will use the above index if following query is fired : SELECT * FROM t1 WHERE a=2 and b IS NULL and c IS NULL and d IS NOT NULL Now my requirement is if i know the index name i.e. index_abcd then is there a way by which i can use it in the select query so that i can be sure that optimizer will pick the index specified(i.e index_abcd) and no other. Some other databases like mySql have this provision using USE INDEX clause I want something similar in postgresql -- View this message in context: http://postgresql.1045698.n5.nabble.com/Using-index-name-in-select-Query-tp5775138p5775143.html Sent from the PostgreSQL - novice mailing list archive at Nabble.com.
I suggest you have a look at EXPLAIN (http://www.postgresql.org/docs/9.3/static/sql-explain.html) if you haven't already. Postgres chooses the best, most efficient way to optimize the query, and so if your index actually has the potential to improve the select query at hand, postgres will choose it. If there is a better way to execute your query than using your index, postgres will not use it.
You should also have a look at Depesz's series on this topic - http://www.depesz.com/tag/unexplainable/ .
On Sat, Oct 19, 2013 at 10:28 AM, naman.iitb <naman.bbps@gmail.com> wrote:
Actually as part of project i have to achieve following task :
suppose i have a table t1(a,b,c,d)
I make an index as :
CREATE INDEX index_abcd on t1(a) WHERE a is NOT NULL and b IS NULL and c
IS NULL and d IS NOT NULL.
There is a high probability but not 100% that optimizer will use the above
index if following query is fired :
SELECT * FROM t1 WHERE a=2 and b IS NULL and c IS NULL and d IS NOT NULL
Now my requirement is if i know the index name i.e. index_abcd then is there
a way by which i can use it in the select query so that i can be sure that
optimizer will pick the index specified(i.e index_abcd) and no other.
Some other databases like mySql have this provision using USE INDEX clause
I want something similar in postgresql
--
View this message in context: http://postgresql.1045698.n5.nabble.com/Using-index-name-in-select-Query-tp5775138p5775143.htmlSent from the PostgreSQL - novice mailing list archive at Nabble.com.
--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Re-read your second post. Not sure why using a particular index is a requirement even if it might lead to a less efficient query. Still, if hints is what you are looking for, then the answer is no, there is no such feature in postgres. On the other hand, if you do want to force postgres to use a particular index, you need to understand how postgres is currently choosing to execute the query, and which index scans are more efficient and being chosen over the index that you want it to use. I guess then all you need to do is delete those indexes so postgres has no choice but to use yours. Although it is definitely not the brightest idea.
On Sat, Oct 19, 2013 at 11:34 AM, Payal Singh <payal@omniti.com> wrote:
I suggest you have a look at EXPLAIN (http://www.postgresql.org/docs/9.3/static/sql-explain.html) if you haven't already. Postgres chooses the best, most efficient way to optimize the query, and so if your index actually has the potential to improve the select query at hand, postgres will choose it. If there is a better way to execute your query than using your index, postgres will not use it.You should also have a look at Depesz's series on this topic - http://www.depesz.com/tag/unexplainable/ .On Sat, Oct 19, 2013 at 10:28 AM, naman.iitb <naman.bbps@gmail.com> wrote:Actually as part of project i have to achieve following task :
suppose i have a table t1(a,b,c,d)
I make an index as :
CREATE INDEX index_abcd on t1(a) WHERE a is NOT NULL and b IS NULL and c
IS NULL and d IS NOT NULL.
There is a high probability but not 100% that optimizer will use the above
index if following query is fired :
SELECT * FROM t1 WHERE a=2 and b IS NULL and c IS NULL and d IS NOT NULL
Now my requirement is if i know the index name i.e. index_abcd then is there
a way by which i can use it in the select query so that i can be sure that
optimizer will pick the index specified(i.e index_abcd) and no other.
Some other databases like mySql have this provision using USE INDEX clause
I want something similar in postgresql
--
View this message in context: http://postgresql.1045698.n5.nabble.com/Using-index-name-in-select-Query-tp5775138p5775143.htmlSent from the PostgreSQL - novice mailing list archive at Nabble.com.
--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Payal Singh-2 wrote >>Postgres chooses the best, most efficient way to optimize >> the query, and so if your index actually has the potential to improve the >> select query at hand, postgres will choose it. If there is a better way >> to >> execute your query than using your index, postgres will not use it. To be fair PostgreSQL is not infallible so if you have a situation where you believe PostgreSQL should be using an index where it is not then such a situation should be documented in detail and sent to the list. You can use various "enable_" GUC to disable sequential scanning and index scanning so that supposedly sub-optimal plans are more likely to be chosen (i.e., you cannot absolutely disable sequential scans since in some cases brute-force is the only valid method to execute a query). Costing parameters can also be altered to better reflect your actual system. In short the project's policy is to fix underlying problems (via code or configuration) rather than implement planner hints that simply allow people to more often shoot themselves in the foot rather than provide a permanent solution. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Using-index-name-in-select-Query-tp5775138p5775148.html Sent from the PostgreSQL - novice mailing list archive at Nabble.com.
On Sat, Oct 19, 2013 at 4:28 PM, naman.iitb <naman.bbps@gmail.com> wrote: > Now my requirement is if i know the index name i.e. index_abcd then is there > a way by which i can use it in the select query so that i can be sure that > optimizer will pick the index specified(i.e index_abcd) and no other. > What you want is called "query hints" and PostgreSQL, as pointed out by others, do not support it by policy. The idea is that the planner knows better than you what to do with your query. What you believe is the better index today could be the bad index tomorrow, or even a few seconds after you just told your boss how smart you have been designing such index, and that is strictly tied to the workload of your cluster. The only way to force PostgreSQL using your own index is to disable all other access methods (e.g., enabled_seqscan = off) and to make all other indexes not available (via catalogs). But this will impact all your cluster. Any chance this is an homework? Luca