473,805 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

index not used?


I'm trying to access a table with about 120M rows. It's a vertical version
of a table with 360 or so columns. The new columns are: original item col,
original item row, and the value.

I created an index:

CREATE INDEX idx on table (col, row)

however, selects are still very slow. It seems it still needs a sequential
scan:

EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
QUERY PLAN
------------------------------------------------------------------------------
Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
Filter: ((col = 1) AND ("row" = 10))

What am I doing wrong?

--
Dan Pelleg

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
6 1644
Scott Marlowe writes:
On Wed, 2004-10-20 at 08:06, Dan Pelleg wrote:
I'm trying to access a table with about 120M rows. It's a vertical version
of a table with 360 or so columns. The new columns are: original item col,
original item row, and the value.

I created an index:

CREATE INDEX idx on table (col, row)

however, selects are still very slow. It seems it still needs a sequential
scan:

EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
QUERY PLAN
------------------------------------------------------------------------------
Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
Filter: ((col = 1) AND ("row" = 10))

What am I doing wrong?


What type are row and col? If they're bigint (i.e. not int / int4) then
you might need to quote the value to get the query to use an index:

SELECT * FROM table WHERE col='1' AND row='10';

also, have you vacuumed / analyzed the table? I'm assuming yes.


They're not bigints:

CREATE TABLE table (col int2, row integer, val double precision)

Yes, I vacuumed and analyzed, right after creating the index. Should I try
and issue a few queries beforehand?

--Dan

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2
On Wed, 2004-10-20 at 08:06, Dan Pelleg wrote:
I'm trying to access a table with about 120M rows. It's a vertical version
of a table with 360 or so columns. The new columns are: original item col,
original item row, and the value.

I created an index:

CREATE INDEX idx on table (col, row)

however, selects are still very slow. It seems it still needs a sequential
scan:

EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
QUERY PLAN
------------------------------------------------------------------------------
Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
Filter: ((col = 1) AND ("row" = 10))

What am I doing wrong?


What type are row and col? If they're bigint (i.e. not int / int4) then
you might need to quote the value to get the query to use an index:

SELECT * FROM table WHERE col='1' AND row='10';

also, have you vacuumed / analyzed the table? I'm assuming yes.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3
On Wed, 2004-10-20 at 09:45, Dan Pelleg wrote:
Scott Marlowe writes:
> On Wed, 2004-10-20 at 08:06, Dan Pelleg wrote:
> > I'm trying to access a table with about 120M rows. It's a vertical version
> > of a table with 360 or so columns. The new columns are: original item col,
> > original item row, and the value.
> >
> > I created an index:
> >
> > CREATE INDEX idx on table (col, row)
> >
> > however, selects are still very slow. It seems it still needs a sequential
> > scan:
> >
> > EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
> > QUERY PLAN
> > ------------------------------------------------------------------------------
> > Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
> > Filter: ((col = 1) AND ("row" = 10))
> >
> > What am I doing wrong?

>
> What type are row and col? If they're bigint (i.e. not int / int4) then
> you might need to quote the value to get the query to use an index:
>
> SELECT * FROM table WHERE col='1' AND row='10';
>
> also, have you vacuumed / analyzed the table? I'm assuming yes.


They're not bigints:

CREATE TABLE table (col int2, row integer, val double precision)

Yes, I vacuumed and analyzed, right after creating the index. Should I try
and issue a few queries beforehand?


but one is an int2 (i.e. not int / int4) so you'll need to quote that
value to get an index to work. Note this is fixed in 8.0 I understand.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4
Scott Marlowe writes:
On Wed, 2004-10-20 at 09:45, Dan Pelleg wrote:
Scott Marlowe writes:
> On Wed, 2004-10-20 at 08:06, Dan Pelleg wrote:
> > I'm trying to access a table with about 120M rows. It's a vertical version
> > of a table with 360 or so columns. The new columns are: original item col,
> > original item row, and the value.
> >
> > I created an index:
> >
> > CREATE INDEX idx on table (col, row)
> >
> > however, selects are still very slow. It seems it still needs a sequential
> > scan:
> >
> > EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
> > QUERY PLAN
> > ------------------------------------------------------------------------------
> > Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
> > Filter: ((col = 1) AND ("row" = 10))
> >
> > What am I doing wrong?
>
> What type are row and col? If they're bigint (i.e. not int / int4) then
> you might need to quote the value to get the query to use an index:
>
> SELECT * FROM table WHERE col='1' AND row='10';
>
> also, have you vacuumed / analyzed the table? I'm assuming yes.


They're not bigints:

CREATE TABLE table (col int2, row integer, val double precision)

Yes, I vacuumed and analyzed, right after creating the index. Should I try
and issue a few queries beforehand?


but one is an int2 (i.e. not int / int4) so you'll need to quote that
value to get an index to work. Note this is fixed in 8.0 I understand.


Bingo.

=> explain select * from table where col='302' and row =100600400;
QUERY PLAN
---------------------------------------------------------------------
Index Scan using idx2 on table (cost=0.00..5.2 7 rows=1 width=14)
Index Cond: ((col = 302::smallint) AND ("row" = 100600400))
(2 rows)

=> explain select * from table where col=302 and row =100600400;
QUERY PLAN
------------------------------------------------------------------------
Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
Filter: ((col = 302) AND ("row" = 100600400))
(2 rows)

Wow, that sure is a big difference for such a small "change" in the
query. Thank you very much!

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #5
Scott Marlowe wrote:
On Wed, 2004-10-20 at 08:06, Dan Pelleg wrote:
I'm trying to access a table with about 120M rows. It's a vertical version
of a table with 360 or so columns. The new columns are: original item col,
original item row, and the value.

I created an index:

CREATE INDEX idx on table (col, row)

however, selects are still very slow. It seems it still needs a sequential
scan:

EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
QUERY PLAN
------------------------------------------------------------------------------
Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
Filter: ((col = 1) AND ("row" = 10))

What am I doing wrong?

What type are row and col? If they're bigint (i.e. not int / int4) then
you might need to quote the value to get the query to use an index:

SELECT * FROM table WHERE col='1' AND row='10';

also, have you vacuumed / analyzed the table? I'm assuming yes.


I assume not, seen that cost...
Regards
Gaetano Mendola

Nov 23 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Scott Marlowe wrote:
| On Fri, 2004-10-22 at 17:11, Gaetano Mendola wrote:
|
|>Scott Marlowe wrote:
|>
|>>On Wed, 2004-10-20 at 08:06, Dan Pelleg wrote:
|>>
|>>
|>>>I'm trying to access a table with about 120M rows. It's a vertical version
|>>>of a table with 360 or so columns. The new columns are: original item col,
|>>>original item row, and the value.
|>>>
|>>>I created an index:
|>>>
|>>>CREATE INDEX idx on table (col, row)
|>>>
|>>>however, selects are still very slow. It seems it still needs a sequential
|>>>scan:
|>>>
|>>>EXPLAIN SELECT * FROM table WHERE col=1 AND row=10;
|>>> QUERY PLAN
|>>>------------------------------------------------------------------------------
|>>>Seq Scan on table (cost=100000000 .00..102612533. 00 rows=1 width=14)
|>>> Filter: ((col = 1) AND ("row" = 10))
|>>>
|>>>What am I doing wrong?
|>>
|>>
|>>What type are row and col? If they're bigint (i.e. not int / int4) then
|>>you might need to quote the value to get the query to use an index:
|>>
|>>SELECT * FROM table WHERE col='1' AND row='10';
|>>
|>>also, have you vacuumed / analyzed the table? I'm assuming yes.
|>
|>I assume not, seen that cost...
|>
|
|
| Actually, that cost would likely be caused by set enable_seqscan = off
| wouldn't it?

That's true. This is the second time in these last days that I see someone "tune"
postgres setting enable_seqscan = off.
G.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBeh6H7Up zwH2SGd4RAvEDAK DdBI6g484jxv4dz dMwXSRwQpJUhgCf U2W7
4hghwH7rJhsC8mR k+Uo/OsU=
=WCBg
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #7

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

Similar topics

6
5738
by: Heiko | last post by:
Hello, is there any way (v$-view) to get informaion about how often an index hast been used since of starting the Database? Thanks for help Heiko
2
5370
by: Mansoor Azam | last post by:
I have the following table with indexes CREATE TABLE dbo.Scratch ( ItemID int IDENTITY (1, 1) NOT NULL , Login varchar (12) NOT NULL , StartDate datetime NULL , PayDate datetime NULL , LastDisconnect datetime NULL
3
3933
by: Phil Latio | last post by:
I am following a book on PHP and MySQL and have come across the below SQL statement. CREATE TABLE users ( user_id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(20) NOT NULL, first_name VARCHAR(15) NOT NULL, last_name VARCHAR(30) NOT NULL, email VARCHAR(40) NULL, password VARCHAR(16) NOT NULL,
14
5426
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB 7.2 environment, the choices the optimizer makes often seem flaky. But this last example really floored me. I was hoping someone could explain why I get worse response time when the optimizer uses two indexes, than when it uses one. Some context:
8
5271
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
29
5489
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
5
17330
by: Bas Scheffers | last post by:
Hi, I have a table with about 100K rows, on which I have created a btree index of the type table_name(int, int, int, timestamp). At first postgres was using it for my AND query on all four columns, but after dropping it and creating different ones and testing, it suddenly stopped using it. Vaccuuming, reindexing, recreating the table and even recreating the database all didn't help.
2
1636
by: Hervé Piedvache | last post by:
Hi, I have may be a stupid question, but I'm a little surprised with some explains I have, using date fields ... I would like to understand exactly when index are used ... I'm using PostgresQL 7.4.1 I have a table with 351 000 records. I have about 300 to 600 new records by day
15
6483
by: rAinDeEr | last post by:
Suppose i have a table which holds thousands of records with the following structure CREATE TABLE "test "."T_CNTRY" ( "CNTRY_CDE" CHAR(2) NOT NULL , "CNTRY_NAME" VARCHAR(50) ) and i have Created an index like below ::
6
3943
by: Henry J. | last post by:
I have a composite index on two columns in a table. However, the index is not used in a query that restricts the 2nd column to a constant. If both columns are linked with columns in other join tables, the index will be used. To illustrate it with an example, I have a query like this: select s.ticker, p.quantity from stock s, positions p where s.type_id = 4
0
9718
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
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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
10369
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
10109
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
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3847
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.