Optimizing Query - Mailing list pgsql-sql

From Justin Long
Subject Optimizing Query
Date
Msg-id 5.0.2.1.0.20010305155556.00afb120@mail.strategicnetwork.org
Whole thread Raw
Responses Re: Optimizing Query  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-sql
Any suggestions welcome!<br /><br /> Here is my query:<br /><br /> select k.*, c.category from knowledge k,
kb_categoriesc , kbwords w0 , kbwords w1 WHERE k.catid=c.catid AND ((k.kbid=w0.kbid and w0.wordid=42743) AND
(k.kbid=w1.kbidand w1.wordid=85369)) ORDER BY k.kbid DESC LIMIT 25;<br /><br /> Now for the details<br /> knowledge k =
3,150records<br /> kbwords = 2-field database (kbid, wordid), 825,748 records<br /><br /> Each word in the knowledge
baseis stored in a database called wordindex, which has 50,000 records or so. The system first explodes the query
stringand pulls the word #s from this database, which is where we get 42743 and 85369 above, "ASIA" and "CHILDREN"
respectively.)The idea is then to pull all the articles in the knowledge base which contain both of these words.<br
/><br/> Here is the EXPLAIN for the query:<br /><br /> NOTICE:  QUERY PLAN:<br /><br /> Nested Loop 
(cost=0.00..527690060.67rows=2878549 width=308)<br />   ->  Nested Loop  (cost=0.00..9472443.40 rows=52582
width=304)<br/>         ->  Nested Loop  (cost=0.00..6278.63 rows=960 width=300)<br />               ->  Index
ScanBackward using knowledge_kbid_key on knowledge k  (cost=0.00..1292.51 rows=2825 width=284)<br />              
-> Seq Scan on kb_categories c  (cost=0.00..1.34 rows=34 width=16)<br />         ->  Seq Scan on kbwords w0 
(cost=0.00..9787.02rows=5474 width=4)<br />   ->  Seq Scan on kbwords w1  (cost=0.00..9787.02 rows=5474 width=4)<br
/><br/> This takes quite a while to return results... prohibitively long. There are indexes on k.catid, c.catid,
k.kbid,w0.kbid, w0.wordid. Any suggestions for further optimization would be very welcome. We get about 3,000 searches
onour database daily...<br /><br /> Blessings,<br /> Justin Long<br /><br /><br /><p><font face="Courier New,
Courier">____________________________________________________________________<br/> Justin
Long                                     Networkfor Strategic Missions<br /> justinlong@strategicnetwork.org 1732 South
ParkCourt<br /><a eudora="autourl"
href="http://www.strategicnetwork.org/">http://www.strategicnetwork.org</a> Chesapeake,VA 23320, USA<br /> Reality
Checke-zine: reality-check-subscribe@yahoogroups.com<br />
____________________________________________________________________<br/> Law: Never retreat. Never surrender. Never
cuta deal with a dragon.<br /> Corollary: No armor? Unclean life? Then do not mess in the affairs <br /> of dragons,
foryou are crunchy and taste good with ketchup.<br /><br /><br />
____________________________________________________________________<br/> Justin
Long                                     Networkfor Strategic Missions<br /> justinlong@strategicnetwork.org 1732 South
ParkCourt<br /><a eudora="autourl"
href="http://www.strategicnetwork.org/">http://www.strategicnetwork.org</a> Chesapeake,VA 23320, USA<br /> Reality
Checke-zine: reality-check-subscribe@yahoogroups.com<br />
____________________________________________________________________<br/> Law: Never retreat. Never surrender. Never
cuta deal with a dragon.<br /> Corollary: No armor? Unclean life? Then do not mess in the affairs <br /> of dragons,
foryou are crunchy and taste good with ketchup.<br /></font> 

pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: random
Next
From: Bruce Momjian
Date:
Subject: Re: PL/SQL-to-PL/PgSQL-HOWTO beta Available