SELECT
items.item_id, items.category_id, items.title, items.description, items.quality,
items.type, items.status, items.price, items.posted, items.modified,
zip_code.state_prefix, zip_code.city, books.isbn13, books.isbn10, books.authors,
books.publisher
FROM
(
(
items
LEFT JOIN bookitems ON items.item_id = bookitems.item_id
)
LEFT JOIN books ON books.isbn13 = bookitems.isbn13
)
LEFT JOIN zip_code ON zip_code.zip_code = items.item_zip
WHERE items.rid = $rid`
I am running this query to get the list of a user's items and their location. The zip_code table has over 40k records and this might be the issue. It currently takes up to 15 seconds to return a list of about 20 items! What can I do to make this query more efficient?
UPDATE: The following is the table creation code for the relevant tables. Sorry about the formatting!
CREATE TABLE
bookitems(
bookitem_idint(10) unsigned NOT NULL auto_increment COMMENT 'BookItem ID',
item_idint(10) unsigned NOT NULL default '0' COMMENT 'Item ID',
isbn13varchar(13) NOT NULL default '' COMMENT 'Book ISBN13',
modifiedtimestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Date of Last Modification',
PRIMARY KEY (bookitem_id),
UNIQUE KEYitem_id(item_id),
KEYfk_bookitems_isbn13(isbn13),
CONSTRAINTfk_bookitems_isbn13FOREIGN KEY (isbn13) REFERENCESbooks(isbn13),
CONSTRAINTfk_bookitems_item_idFOREIGN KEY (item_id) REFERENCESitems(item_id)
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=latin1;CREATE TABLE
books(
isbn13varchar(13) NOT NULL default '' COMMENT 'Book ISBN13 (pk)',
isbn10varchar(10) NOT NULL default '' COMMENT 'Book ISBN10 (u)',
titletext NOT NULL COMMENT 'Book Title',
title_longtext NOT NULL,
authorstext NOT NULL COMMENT 'Book Authors',
publishertext NOT NULL COMMENT 'ISBNdb publisher_text',
PRIMARY KEY (isbn13),
UNIQUE KEYisbn10(isbn10)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE
items(
item_idint(10) unsigned NOT NULL auto_increment COMMENT 'Item ID',
ridint(10) unsigned NOT NULL default '0' COMMENT 'Item Owner User ID',
category_idint(10) unsigned NOT NULL default '0' COMMENT 'Item Category ID',
titletinytext NOT NULL COMMENT 'Item Title',
descriptiontext NOT NULL COMMENT 'Item Description',
qualityenum('0','1','2','3','4','5') NOT NULL default '0' COMMENT 'Item Quality',
typeenum('forsale','wanted','pending') NOT NULL default 'pending' COMMENT 'Item Status',
priceint(6) unsigned NOT NULL default '0' COMMENT 'Price',
posteddatetime NOT NULL default '0000-00-00 00:00:00' COMMENT 'Date of Listing',
modifiedtimestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'Date of Last Modification',
statusenum('sold','found','flagged','removed','active','expired') NOT NULL default 'active',
item_zipint(5) unsigned zerofill NOT NULL default '00000',
PRIMARY KEY (item_id),
KEYfk_items_rid(rid),
KEYfk_items_category_id(category_id),
CONSTRAINTfk_items_category_idFOREIGN KEY (category_id) REFERENCEScategories(category_id),
CONSTRAINTfk_items_ridFOREIGN KEY (rid) REFERENCESusers(rid)
) ENGINE=InnoDB AUTO_INCREMENT=123 DEFAULT CHARSET=latin1;CREATE TABLE
users(
ridint(10) unsigned NOT NULL auto_increment COMMENT 'User ID',
fidint(10) unsigned NOT NULL default '0' COMMENT 'Facebook User ID',
role_idint(10) unsigned NOT NULL default '4',
zipint(5) unsigned zerofill NOT NULL default '00000' COMMENT 'Zip Code',
joinedtimestamp NOT NULL default CURRENT_TIMESTAMP COMMENT 'INSERT Timestamp',
notesvarchar(255) NOT NULL default '',
PRIMARY KEY (rid),
UNIQUE KEYfid(fid),
KEYfk_users_role(role_id),
CONSTRAINTfk_users_roleFOREIGN KEY (role_id) REFERENCESroles(role_id)
) ENGINE=InnoDB AUTO_INCREMENT=1013 DEFAULT CHARSET=latin1;CREATE TABLE
zip_code(
idint(11) unsigned NOT NULL auto_increment,
zip_codevarchar(5) character set utf8 collate utf8_bin NOT NULL,
cityvarchar(50) character set utf8 collate utf8_bin default NULL,
countyvarchar(50) character set utf8 collate utf8_bin default NULL,
state_namevarchar(50) character set utf8 collate utf8_bin default NULL,
state_prefixvarchar(2) character set utf8 collate utf8_bin default NULL,
area_codevarchar(3) character set utf8 collate utf8_bin default NULL,
time_zonevarchar(50) character set utf8 collate utf8_bin default NULL,
latfloat NOT NULL,
lonfloat NOT NULL,
search_stringvarchar(52) NOT NULL default '',
PRIMARY KEY (id),
KEYzip_code(zip_code)
) ENGINE=MyISAM AUTO_INCREMENT=42625 DEFAULT CHARSET=utf8;