Author Topic: Large table and query time  (Read 1626 times)

0 Members and 1 Guest are viewing this topic.

Offline jaymcTopic starter

  • Addict
  • Posts: 1,528
  • Gender: Male
    • View Profile
    • SEO Agency
Large table and query time
« on: August 12, 2008, 09:23:23 AM »
Im having some trouble with a big table I have

It has 4.7 million records and is heavily accessed

It has the following fields

id = PRIMARY
user = varchar25 INDEX
friend = varchar25 INDEX
timestamp = int12
gender = varchar6
x = tinyint
real = enum 0,1


I cant prune redundant records because.. well basically there is none

Running SELECT count(id) FROM table GROUP BY user takes 5 minutes+
I deleted an index on the x colum and that took 28 minutes to complete (ALTER TABLE)[/b]

What options do I have in circumstances of naturally large tables


The example of SELECT count(id) FROM table GROUP BY user above, is there anything I can do to get something
like that complete after 5 seconds?
I would love to change the world, but they won't give me the source code

SEO Agency

Offline Xurion

  • Enthusiast
  • Posts: 258
    • View Profile
Re: Large table and query time
« Reply #1 on: August 12, 2008, 09:44:08 AM »
What type of engine is the table on? Myisam? Innodb?

Offline jaymcTopic starter

  • Addict
  • Posts: 1,528
  • Gender: Male
    • View Profile
    • SEO Agency
Re: Large table and query time
« Reply #2 on: August 12, 2008, 09:46:22 AM »
Sorry

INNODB
I would love to change the world, but they won't give me the source code

SEO Agency

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Large table and query time
« Reply #3 on: August 12, 2008, 04:06:29 PM »
Why not a covering index on ( user, id )?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline jaymcTopic starter

  • Addict
  • Posts: 1,528
  • Gender: Male
    • View Profile
    • SEO Agency
Re: Large table and query time
« Reply #4 on: August 12, 2008, 04:12:26 PM »
Sorry, bad example

id is never ever used, apart from in my sample query.. so a covering index on that would prove useless
I would love to change the world, but they won't give me the source code

SEO Agency

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Large table and query time
« Reply #5 on: August 12, 2008, 04:14:58 PM »
Sorry, bad example

id is never ever used, apart from in my sample query.. so a covering index on that would prove useless
I don't understand.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline jaymcTopic starter

  • Addict
  • Posts: 1,528
  • Gender: Male
    • View Profile
    • SEO Agency
Re: Large table and query time
« Reply #6 on: August 12, 2008, 05:04:07 PM »
on my website I never use the id field

however, in the example I gave you above of a bad query I used the id field leading you to believe i need a covering index on it
I would love to change the world, but they won't give me the source code

SEO Agency

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Large table and query time
« Reply #7 on: August 12, 2008, 05:19:58 PM »
on my website I never use the id field

however, in the example I gave you above of a bad query I used the id field leading you to believe i need a covering index on it

Why are we talking about imaginary queries?  I still don't understand.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline jaymcTopic starter

  • Addict
  • Posts: 1,528
  • Gender: Male
    • View Profile
    • SEO Agency
Re: Large table and query time
« Reply #8 on: August 12, 2008, 05:49:41 PM »
Well I want to know why this

SELECT count(id) FROM table GROUP BY user

takes over 5 minutes to complete when id is the primary key
I would love to change the world, but they won't give me the source code

SEO Agency

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Large table and query time
« Reply #9 on: August 13, 2008, 07:54:05 PM »
Why count(id) and not count(*)?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline awpti

  • Enthusiast
  • Posts: 453
  • Gender: Male
    • View Profile
    • GoMySQL - DBA Blog and Tutorials
Re: Large table and query time
« Reply #10 on: August 14, 2008, 01:07:02 AM »
Why are you using a group by?

That makes no sense.

SELECT COUNT(*) FROM my_table; will return the same total number.

How are you defining which group a given count belongs to?

I'd suggest creating an index:

 ALTER TABLE `my_table` ADD INDEX ( `user` ( 25 ) ) ;

Reduced the count query time from 1m 32s to ~20sec. I'm guessing your db server isn't a terribly beefy machine.

As a side note, I forced in double the amount of records you have, all with random data.
« Last Edit: August 14, 2008, 01:09:11 AM by awpti »
Server: Apache 2.2.3 - PHP 5.2.17, MySQL 5.0, 5.1, 5.5 and 6.0 - 2x Quad Core Xeon 5620 w/ 16G Mem
GeekLAN - Ignited Jobs - LAMP Tips

10+ Years of hobby PHP Development and Database Design.

Offline toplay

  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,272
    • View Profile
Re: Large table and query time
« Reply #11 on: August 14, 2008, 01:25:11 AM »
You could run optimize periodically:

http://dev.mysql.com/doc/refman/5.0/en/optimize-table.html

and:

http://dev.mysql.com/doc/refman/5.0/en/analyze-table.html


COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause.

This optimization applies only to MyISAM tables only, because an exact row count is stored for this storage engine and can be accessed very quickly. For transactional storage engines such as InnoDB and BDB, storing an exact row count is more problematic because multiple transactions may be occurring, each of which may affect the count.

Offline jaymcTopic starter

  • Addict
  • Posts: 1,528
  • Gender: Male
    • View Profile
    • SEO Agency
Re: Large table and query time
« Reply #12 on: August 14, 2008, 05:27:46 AM »
I dont think optimize will help as they are INNODB tables thus zero/little overhead. I've only experienced fregmented tables with MyIsam

If ran optimize on the 4.7 million rows table it would take about 10-20 minutes

I already have an index on user

SELECT count(*) FROM table GROUP BY user
This is an example of a query that is taking 400+ seconds to complete. Ignore the relivence of it, as its only an example and not something I ever use, but if I can find out why a simple query like this takes so long..
« Last Edit: August 14, 2008, 05:28:23 AM by jaymc »
I would love to change the world, but they won't give me the source code

SEO Agency

Offline Hooker

  • Enthusiast
  • Posts: 228
  • Gender: Male
    • View Profile
Re: Large table and query time
« Reply #13 on: August 14, 2008, 06:12:46 AM »
Doing a "SELECT Count(*) FROM table" on InnoDB and requires a full table scan. (With MyIsam this operation doesn't cost anything because MyIsam stores an internal record counter with each table).

Offline awpti

  • Enthusiast
  • Posts: 453
  • Gender: Male
    • View Profile
    • GoMySQL - DBA Blog and Tutorials
Re: Large table and query time
« Reply #14 on: August 14, 2008, 06:31:25 AM »
Your DB design here is full of fail.

Do you handle tons of inserts/updates to this table? If not, switch the engine to isam.
Server: Apache 2.2.3 - PHP 5.2.17, MySQL 5.0, 5.1, 5.5 and 6.0 - 2x Quad Core Xeon 5620 w/ 16G Mem
GeekLAN - Ignited Jobs - LAMP Tips

10+ Years of hobby PHP Development and Database Design.