2

I have table storing phone numbers with 800M rows.

column
region_code_id  smallint(4)  unsigned   YES         
local_number    mediumint(7) unsigned   YES         
region_id       smallint(4)  unsigned   YES         
operator_id     smallint(4)  unsigned   YES         
id  int(10)     unsigned     NO PRI     auto_increment

I need find number.id where region_code_id = 119 and localnumber = 1234567

select * from numbers where numbers.region_code_id = 119 and numbers.local_number = 1234567;

this query execute over 600 second. How can I improve it ?

UPDATE

Thank for unswer, i understand i need index for this column, i try this as soon as I get the server with more SSD, now i have free 1GB SSD space. How i can to find out how much space the index will occupy?

2
  • 1
    Do you have an index on (region_code_id, local_number) ? Mar 21, 2016 at 10:56
  • Are you searching on indexed columns? How large is the table, and is it able to fit into memory for faster searching? What are the server settings for the database? What sort of hardware is it running on?
    – gabe3886
    Mar 21, 2016 at 10:57

3 Answers 3

3

Consider adding INDEX on columns which you use in WHERE clause.

Start with:

ALTER TABLE `numbers`
    ADD INDEX `region_code_id_local_number` 
    (`region_code_id`, `local_number`);

Note : it can take some time for index to build.

Before and after change, execute explain plan to compare:

EXPLAIN EXTENDED select * from numbers where numbers.region_code_id = 119 and numbers.local_number = 1234567;

References:

How MySQL uses indexes

3
  • Thank, i try this before as soon as I get the server with more SSD, now i have free 1GB SSD space. How i can to find out how much space the index will occupy?
    – berap
    Mar 21, 2016 at 11:09
  • Now i have index for id column yadi.sk/i/Mgd8eIHzqMXy9 can I delete it if an id is not needed?
    – berap
    Mar 21, 2016 at 11:11
  • I would not drop PRIMARY key. Make a dump/backup of DB and re-import it after you enlarge free disk space.
    – rkosegi
    Mar 21, 2016 at 11:55
1

For this query:

select *
from numbers
where numbers.region_code_id = 119 and
      numbers.local_number = 1234567;

You want an index on numbers(region_code_id, local_number) or numbers(local_number, region_code_id). The order of the columns doesn't matter because the conditions are equality for both columns.

create index idx_numbers_region_local on numbers(region_code_id, local_number);
0

I agree that INDEX(region_code_id, local_number) (in either order) is mandatory for this problem, but I am sticking my nose in to carry it a step further. Isn't that pair "unique"? Or do you have duplicate numbers in the table? If it is unique, then get rid of id and make that pair PRIMARY KEY(region_code_id, local_number). The table will possibly be smaller after the change.

Back to your question of "how big". How big is the table now? Perhaps 40GB? A secondary index (as originally proposed) would probably add about 20GB. And you would need 20-60GB of free disk space to perform the ALTER. This depends on whether adding the index can be done "inplace" in that version.

Changing the PK (as I suggest) would result in a little less than 40GB for the table. It will take 40GB of free space to perform the ALTER.

In general (and pessimistically), plan on an ALTER needing the both the original table and the new table sitting on disk at one time. That includes full copies of the data and index(es).

(A side question: Are you sure local_number is limited to 7 digits everywhere?)

Another approach to the question... For calculating the size of a table or index in InnoDB, add up the datatype sizes (3 bytes for MEDIUMINT, some average for VARCHAR, etc). Then multiply by the number of rows. Then multiply by 4; this will give you the approximate disk space needed. (Usually 2-3 is sufficient for the last multiplier.)

When changing the PK, do it in one step:

ALTER TABLE foo
    DROP PRIMARY KEY,
    ADD  PRIMARY KEY(region_code_id, local_number);

Changing the PK cannot be done "inplace".

Edit (mostly for other readers)

@berap points out that id is needed for other purposes. Hence, dropping id and switching the PK is not an option.

However, this is sometimes an option (perhaps not in this case):

ALTER TABLE foo
    DROP PRIMARY KEY,
    ADD  PRIMARY KEY(region_code_id, local_number),
    ADD  INDEX(id);

Notes:

  • The id..AUTO_INCREMENT will continue to work even with just INDEX.
  • The SELECT in question will be more efficient because it is the PK.
  • SELECT .. WHERE id = ... will be less efficient because id is a secondary key.
  • The table will be the same size either way; the secondary key would also be the same size either way -- because every secondary key contains the PK columns, too. (This note is InnoDB-specific.)
3
  • Hello, Thank you for answer
    – berap
    Mar 22, 2016 at 17:38
  • Pair 'region_code_id', 'local_number' is "unique" but i have the table 'number_comments' where comment associate by 'numbers.id'
    – berap
    Mar 22, 2016 at 17:45
  • OK. I added some more stuff. Proceed with adding INDEX(region_code_id, local_number) as the best option.
    – Rick James
    Mar 22, 2016 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.