Unix Technical Forum

Querying large table

This is a discussion on Querying large table within the MySQL General forum forums, part of the MySQL category; --> Hey, guys. I have 2 tables: categories and items. COUNT(*) categories = 63 833 COUNT(*) items = 742 993 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:06 AM
Shadow
 
Posts: n/a
Default Querying large table

Hey, guys.

I have 2 tables: categories and items.
COUNT(*) categories = 63 833
COUNT(*) items = 742 993
I need to get number of items in a specific category, so I use
SELECT COUNT(*) FROM items WHERE ctg='<ctg>'

But each query takes ~ 10seconds.
Its really slow.

Can anybody propose some optimization?

Thanks.


---------------------------------
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:06 AM
Maciej Dobrzanski
 
Posts: n/a
Default Re: Querying large table

In news:907873.28994.qm@web52811.mail.re2.yahoo.com,
Shadow <shadow_developer@yahoo.com> wrote:

> I need to get number of items in a specific category, so I use
> SELECT COUNT(*) FROM items WHERE ctg='<ctg>'
>
> But each query takes ~ 10seconds.
> Its really slow.


You may add an index on `items`.`ctg` if there is none.

Maciek

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:06 AM
Brent Baisley
 
Posts: n/a
Default Re: Querying large table

It's taking a long time because your filter is external to the table, so you can't use an index. You want to focus your search on
the category table, where you can quickly narrow down the number of records to search.

SELECT ctg, count(itemid) FROM categories JOIN items ON ctgID=itemCtgID WHERE ctg='<ctg>' GROUP BY ctg;


----- Original Message -----
From: "Shadow" <shadow_developer@yahoo.com>
To: <mysql@lists.mysql.com>
Sent: Thursday, March 29, 2007 6:59 PM
Subject: Querying large table


> Hey, guys.
>
> I have 2 tables: categories and items.
> COUNT(*) categories = 63 833
> COUNT(*) items = 742 993
> I need to get number of items in a specific category, so I use
> SELECT COUNT(*) FROM items WHERE ctg='<ctg>'
>
> But each query takes ~ 10seconds.
> Its really slow.
>
> Can anybody propose some optimization?
>
> Thanks.
>
>
> ---------------------------------
> Finding fabulous fares is fun.
> Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:27 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com