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

Home Posts Topics Members FAQ

Index usage



Hi. I'm having some trouble on the use of indexes.

The querys below are exactly the same but refer to different months.
One case uses indexes, the other doesn't.

Is there anything I can do? Increasing index mem size?

Query 2 hash 9105 entries matching the given conditions
Query 2 hash 9248 entries matching the given conditions

QUERY 1:

explain select ra_datacolh::da te, count(distinct ra_utente) from requisicaoanali se where (ra_datacolh::d ate >= '2003-4-01'::date and ra_datacolh::da te < '2003-5-1'::date) and isactive=0 group by ra_datacolh;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=0.00..123 00.68 rows=313 width=8)
-> Group (cost=0.00..122 92.86 rows=3130 width=8)
-> Index Scan using requisicaoanali se_datacolh on requisicaoanali se (cost=0.00..122 85.03 rows=3130 width=8)
Index Cond: ((ra_datacolh >= '2003-04-01'::date) AND (ra_datacolh < '2003-05-01'::date))
Filter: (isactive = 0)

QUERY 2:

netlab=> explain select ra_datacolh::da te, count(distinct ra_utente) from requisicaoanali se where (ra_datacolh::d ate >= '2003-6-01'::date and ra_datacolh::da te < '2003-7-1'::date) and isactive=0 group by ra_datacolh;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=32452.98. .32632.06 rows=2388 width=8)
-> Group (cost=32452.98. .32572.36 rows=23877 width=8)
-> Sort (cost=32452.98. .32512.67 rows=23877 width=8)
Sort Key: ra_datacolh
-> Seq Scan on requisicaoanali se (cost=0.00..307 16.71 rows=23877 width=8)
Filter: ((ra_datacolh >= '2003-06-01'::date) AND (ra_datacolh < '2003-07-01'::date) AND (isactive = 0))


--
Pedro Miguel G. Alves pm*****@think.p t
THINK - Tecnologias de Informação www.think.pt
Tel: +351 21 413 46 00 Av. José Gomes Ferreira
Fax: +351 21 413 46 09 nº 13 1495-139 ALGÉS

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 11 '05 #1
5 1879
On Mon, 25 Aug 2003, Pedro Alves wrote:
The querys below are exactly the same but refer to different months.
One case uses indexes, the other doesn't.

Is there anything I can do? Increasing index mem size?


Run "vacuum analyze". The planner seems to think that one of the queries
returns 313 rows while the other returns 2388 rows.

To me that looks like the statistics need to be updated using vacuum
analyze.

Also, explain analyze gives a little more information and is better to
run then just explain.

--
/Dennis
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 11 '05 #2

One more thing I just noticed. Right after making a vacuum analyze on
the table, I saw the following:

Seq scan:

- Cost: 10484
- Timing: 624ms

Index scan (with enable_seqscan = false):

- Cost: 10628
- Timing: 41ms
In production state the query goes up to a minute long (I ran this in a
test database) and it takes less than a second using indexes. What can be
causing this?
Is it safe to turn enable_seqscan = false in production environment?

Thanks

On Tue, Aug 26, 2003 at 09:59:35AM +0100, Pedro Alves wrote:

First of all tkx for the answer, Dennis.

The vacuum analyze is run on daily basis, so that cannot be the point.

Is there any way to force the planner to use the index?
Bellow is the explain analyze of the querys. Indeed, the second range has
more rows (9105 / 21503), but is this SO big that the planner cannot
handle?
This is running in a dedicated machine with 512Mb ram. Is there any
configuration parameter so that I can increase the "index to seq turn point"? :)
Thanks
OK _______________ _______________ ____

explain ANALYZE select count(1) from requisicaoanali se where
(ra_datacolh::d ate >= '2003-4-01'::date and ra_datacolh::da te <
'2003-5-1'::date) and isactive=0;

Aggregate (cost=10660.84. .10660.84 rows=1 width=0) (actual
time=172.41..17 2.41 rows=1 loops=1)
-> Index Scan using requisicaoanali se_datacolh on requisicaoanali se (cost=0.00..106 54.06 rows=2711 width=0) (actual time=0.13..145. 50 rows=9105 loops=1)
Index Cond: ((ra_datacolh >= '2003-04-01'::date) AND (ra_datacolh
< '2003-05-01'::date))
Filter: (isactive = 0)
Total runtime: 172.62 msec
(5 rows)

NOK _______________ _______________ ____

explain ANALYZE select count(1) from requisicaoanali se where (ra_datacolh::d ate >= '2003-6-01'::date and ra_datacolh::da te < '2003-7-1'::date) and isactive=0;

Aggregate (cost=31019.00. .31019.00 rows=1 width=0) (actual time=43252.40.. 43252.40 rows=1 loops=1)
-> Seq Scan on requisicaoanali se (cost=0.00..309 65.24 rows=21503 width=0) (actual time=8.43..4322 4.01 rows=9248 loops=1)
Filter: ((ra_datacolh >= '2003-06-01'::date) AND (ra_datacolh < '2003-07-01'::date) AND (isactive = 0))
Total runtime: 43252.57 msec
(4 rows)

On Tue, Aug 26, 2003 at 08:37:08AM +0200, Dennis Björklund wrote:
On Mon, 25 Aug 2003, Pedro Alves wrote:
The querys below are exactly the same but refer to different months.
One case uses indexes, the other doesn't.

Is there anything I can do? Increasing index mem size?


Run "vacuum analyze". The planner seems to think that one of the queries
returns 313 rows while the other returns 2388 rows.

To me that looks like the statistics need to be updated using vacuum
analyze.

Also, explain analyze gives a little more information and is better to
run then just explain.


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org


--
Pedro Miguel G. Alves pm*****@think.p t
THINK - Tecnologias de Informação www.think.pt
Tel: +351 21 413 46 00 Av. José Gomes Ferreira
Fax: +351 21 413 46 09 nº 13 1495-139 ALGÉS

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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

Nov 11 '05 #3
Pedro Alves <pm*****@think. pt> writes:
It's the same even with statistics=1000 .


Uh, you did actually ANALYZE the table after each change, right?
Doesn't the EXPLAIN output change at all?

regards, tom lane

---------------------------(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 11 '05 #4
Pedro Alves <pm*****@think. pt> writes:
The vacuum analyze is run on daily basis, so that cannot be the point.


The next thing I'd try to improve the planner's guess is to increase the
statistics target for the ra_datacolh column (see ALTER TABLE SET
STATISTICS). It looks like the default of 10 is too small for that
column --- try 100 and see if the rowcount estimates get any closer.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 11 '05 #5
Look through the docs. By altering the values of random_page_cos t,
effect_cache_si ze and cpu_tuple_cost you can make the estimates approximate
real life better.

On Tue, Aug 26, 2003 at 12:32:23PM +0100, Pedro Alves wrote:



One more thing I just noticed. Right after making a vacuum analyze on
the table, I saw the following:

Seq scan:

- Cost: 10484
- Timing: 624ms

Index scan (with enable_seqscan = false):

- Cost: 10628
- Timing: 41ms


In production state the query goes up to a minute long (I ran this ina
test database) and it takes less than a second using indexes. What can be
causing this?


Is it safe to turn enable_seqscan = false in production environment?



Thanks



On Tue, Aug 26, 2003 at 09:59:35AM +0100, Pedro Alves wrote:

First of all tkx for the answer, Dennis.

The vacuum analyze is run on daily basis, so that cannot be the point.

Is there any way to force the planner to use the index?


Bellow is the explain analyze of the querys. Indeed, the second range has
more rows (9105 / 21503), but is this SO big that the planner cannot
handle?


This is running in a dedicated machine with 512Mb ram. Is there any
configuration parameter so that I can increase the "index to seq turn point"? :)


Thanks


OK _______________ _______________ ____

explain ANALYZE select count(1) from requisicaoanali se where
(ra_datacolh::d ate >= '2003-4-01'::date and ra_datacolh::da te <
'2003-5-1'::date) and isactive=0;

Aggregate (cost=10660.84. .10660.84 rows=1 width=0) (actual
time=172.41..17 2.41 rows=1 loops=1)
-> Index Scan using requisicaoanali se_datacolh on requisicaoanali se(cost=0.00..1 0654.06 rows=2711 width=0) (actual time=0.13..145. 50rows=9105 loops=1)
Index Cond: ((ra_datacolh >= '2003-04-01'::date) AND (ra_datacolh
< '2003-05-01'::date))
Filter: (isactive = 0)
Total runtime: 172.62 msec
(5 rows)



NOK _______________ _______________ ____

explain ANALYZE select count(1) from requisicaoanali se where (ra_datacolh::d ate >= '2003-6-01'::date and ra_datacolh::da te < '2003-7-1'::date) and isactive=0;

Aggregate (cost=31019.00. .31019.00 rows=1 width=0) (actual time=43252.40.. 43252.40 rows=1 loops=1)
-> Seq Scan on requisicaoanali se (cost=0.00..309 65.24 rows=21503 width=0) (actual time=8.43..4322 4.01 rows=9248 loops=1)
Filter: ((ra_datacolh >= '2003-06-01'::date) AND (ra_datacolh < '2003-07-01'::date) AND (isactive = 0))
Total runtime: 43252.57 msec
(4 rows)





On Tue, Aug 26, 2003 at 08:37:08AM +0200, Dennis Björklund wrote:
On Mon, 25 Aug 2003, Pedro Alves wrote:

> The querys below are exactly the same but refer to different months.
> One case uses indexes, the other doesn't.
>
> Is there anything I can do? Increasing index mem size?

Run "vacuum analyze". The planner seems to think that one of the queries
returns 313 rows while the other returns 2388 rows.

To me that looks like the statistics need to be updated using vacuum
analyze.

Also, explain analyze gives a little more information and is better to
run then just explain.



---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org


--
Pedro Miguel G. Alves pm*****@think.p t
THINK - Tecnologias de Informação www.think.pt
Tel: +351 21 413 46 00 Av. José Gomes Ferreira
Fax: +351 21 413 46 09 nº 13 1495-139 ALGÉS

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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


--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ "All that is needed for the forces of evil to triumph is for enough good
men to do nothing." - Edmond Burke
"The penalty good people pay for not being interested in politics is to be
governed by people worse than themselves." - Plato


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE/S1rQY5Twig3Ge+Y RAqv4AKC+oQx6Xz 1f/SDQcaYyfLCHwID1 qQCeLn/o
6gf9kpEGHVblz1Q jgnc3/+0=
=mXMg
-----END PGP SIGNATURE-----

Nov 11 '05 #6

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

Similar topics

10
3681
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
6
5731
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
0
2808
by: Guy Deprez | last post by:
Hi, i'm having a problem to create indexes. STEP 1 ----------- Connection is OK (you can find the string at the end of the message) Table ("Couleurs") creation is OK STEP 2. Index Creation
8
4834
by: Andr? Queiroz | last post by:
Hi, I have a table with 10M records and col A has a index created on it. The data on that table has the same value for col A on all 10M records. After that I insert diferent values for that column but my queries do not use the index I created for that column. Is there any way I can force the usage of the index or to ommit a value on the index creation, like 0 (zeroes) or spaces? Thanks in advance, André Queiroz
8
5241
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,...
2
1864
by: SKC | last post by:
Is there a way to find the following in UDB ? 1. How many times an index is used for a duration ? 2. Which SQL/application is using the index? if not, is there a different way to find it ?
12
2820
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage && function_exists('imagecreatetruecolor') && preg_match('/2\.0/i', $this->gd_info_array)) { $newImage = @imagecreatetruecolor($configArray, $configArray);
35
29216
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. import shutil, os, sys
13
7704
by: Eugene Rice | last post by:
I'm writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places: char arr; char x,y,z; for (x=0; x<3; x++) { arr = ...; // gcc gives warning here // more code }
7
2752
by: Henry J. | last post by:
I got a dumb question on the merge statement. I read the following example of merge statement at the IBM page: http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0010873.htm: MERGE INTO archive ar USING (SELECT activity, description FROM activities) ac ON (ar.activity = ac.activity) WHEN MATCHED THEN UPDATE SET description = ac.description
0
8403
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
8316
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
8737
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...
0
7345
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
5636
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
4168
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
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.