Attila
Attila

Reputation: 3406

MySQL - UPDATE + Query optimalization

I'am having an optimalization problem. I am not really into MySQL Indexes yet. I read a few articles and questions but I haven't found what I am looking for.

I have an UPDATE statement with a WHERE condition on a table having nearly 1 million rows.

I want to speed up this UPDATE operation. I think most of the time is spent by looking for a the record specified in the WHERE.

My queries look like this, with different URLs:

UPDATE table SET updated = 1, updated_date = now() , [some more fields updated]
WHERE url = 'someurl.com?id=123532js23';

URLs are unique. Currently there is an AUTO INREMENT id field defined as a PRIMARY KEY. I dont need any <, > or BETWEEN operations - so maybe could I use some hashing?

What engine and indexing should I use for best performance?

One of my friends suggested to use InnoDB + UNIQUE on url field. Is there anything else I can do? This UPDATE runs really many times - about 1 000 000 times each day and most of the executions results in updating - about 95%.

Thanks for any help!

Upvotes: 1

Views: 48

Answers (1)

Quassnoi
Quassnoi

Reputation: 425361

One of my friends suggested to use InnoDB + UNIQUE on url field. Is there anything else I can do? This UPDATE runs really many times - about 1 000 000 times each day and most of the executions results in updating - about 95%.

You friend is right.

One thing is that URL may be long and the maximum possible length of an index key on InnoDB is 767 bytes.

Thus, you would better hash the urls (say, with MD5) and create a UNIQUE index on the field containing the url's hash (and of course use it in the WHERE condition):

INSERT
INTO    mytable (url, hashed_url, ...)
VALUES  ('someurl.com?id=123532js23', MD5('someurl.com?id=123532js23'))

UPDATE  mytable
SET     ...
WHERE   hashed_url = MD5('someurl.com?id=123532js23')

Upvotes: 3

Related Questions