0

Simplifying the database/table structure i have a situation with two tables where we store 'items' and item properties (the relation between the two is 1-N)

I'm trying to optimize the following query, which fetches latest items being in the hotdeals section. To do that we have item_property table which stores items sections along with many other item metadata

NOTE: table structure can't be changed to optimize the query, ie: we can't simply add the section as a column in the item table as we can have unlimited amount of sections for each item.

Here's the Structure of both tables:

CREATE TABLE `item` (
  `iditem` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `itemname` varchar(200) NOT NULL,
  `desc` text NOT NULL,
  `ok` int(11) NOT NULL DEFAULT '10',
  `date_created` datetime NOT NULL,
  PRIMARY KEY (`iditem`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `item_property` (
  `iditem` int(11) unsigned NOT NULL,
  `proptype` varchar(64) NOT NULL DEFAULT '',
  `propvalue` varchar(200) NOT NULL DEFAULT '',
  KEY `iditem` (`iditem`,`proptype`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

And here's the query:

SELECT *
FROM item 
JOIN item_property ON item.iditem=item_property.iditem 
WHERE 
    item.ok > 70 
    AND item_property.proptype='section'
    AND item_property.propvalue = 'hotdeals' 

ORDER BY item.date_created desc
LIMIT 20

Which would be the best indexes to optimize this query? Right now the optimizer (Explain) will use temporary and filesort, processing a Ton of rows (the size of the join)

Tables are both MyIsam at the moment, but can be changed to InnoDB if its really necessary to optimize the queries

Thanks

3
  • You need to show us the table and index definitions. Diagnosing slow queries requires full table and index definitions, not just a description or paraphrase. Maybe your tables are defined poorly. Maybe the indexes aren't created correctly. Maybe you don't have an index on that column you thought you did. Without seeing the table and index definitions, we can't tell. If you know how to do an EXPLAIN or get an execution plan, put the results in the question as well. May 22, 2013 at 14:15
  • Hi, my tables have a complex structure, and would be overkill to add those here, thats why i used a simplified example, suppose that there are no indexes, and the question was more to understand how the optimizer engine would work for a query like this, so being able to enhance the performance of many queries like this
    – jmserra
    May 22, 2013 at 14:53
  • Anyway, just added table creation queries to define their basic structure
    – jmserra
    May 22, 2013 at 15:41

2 Answers 2

1

What is the type of item_property.idOption and item_property.type columns? If they contain a limited number of options - make them ENUM (if they are not already). Enum values are indexed automatically. And (of course) you should have item_property.iditem and item.date_created columns indexed also. This will increase the size of the tables, but will considerably fasten the queries that join and sort by these fields.

1
  • Sorry but they are dynamic, meant to hold unlimited values, so ENUM won't be an option for our environment, just updated the question with table definition, thanks!
    – jmserra
    May 22, 2013 at 15:42
1

A note about data correctness:

One of the big benefits of a NOT NULL is to prevent your program from creating a row that doesn't have all columns properly specified. Having a DEFAULT renders that useless.

Is it ever OK to have a blank proptype or propvalue? What does a blank in those fields mean? If it's OK to not have a proptype set, then remove the NOT NULL constraint. If you must always have a proptype set, then having DEFAULT '' will not save you from the case of inserting into the row but forgetting to set proptype.

In most cases, you want either NOT NULL or DEFAULT 'something' on your columns, but not both.

1
  • Thanks, but actually just created those tables in a GUI Tool and got the creation syntax from there, so in this case it won't be relevant, what i would like to do instead is to optimize the query times, and minimize the amount of processed rows, do you have any tip for that?
    – jmserra
    May 23, 2013 at 12:00

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.