View Single Post
Old 11-24-2012, 05:45 PM   PM User | #1
shoryuken
New Coder

 
Join Date: May 2012
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
shoryuken is an unknown quantity at this point
Query help with a poorly structured table

I have a table and query which is causing performance issues. It was fine when small, but now has about 6 million rows in production and it's growing fast. Before I start rewriting stuff or creating an archive I am wondering if there is a quick win possible here with better indexing and would be very thankful if anyone can help I think there is a quick fix because the query is fast without the "AND visible = 'Y'", but I need help if anyone can please

My profiling tool tells me "Very bad, very slow: Join uses key prefix or non-unique key. Key compared with = or <=>)".

The query is:

Code:
SELECT type FROM orderhistory WHERE (order_id = '943581' OR ticket_id = '943581') AND visible = 'Y' limit 1
Table structure:

Code:
CREATE TABLE `orderhistory` (
  `uniqueid` int(11) NOT NULL AUTO_INCREMENT,
  `order_id` int(16) NOT NULL,
  `ticket_id` int(16) NOT NULL,
  `type` char(8) NOT NULL,
  `date` int(11) unsigned NOT NULL,
  `notes` text NOT NULL,
  `ontime` char(1) NOT NULL,
  `postedby` varchar(96) NOT NULL,
  `visible` char(1) NOT NULL DEFAULT 'Y',
  `ip` char(16) NOT NULL,
  PRIMARY KEY (`uniqueid`),
  KEY `postedby` (`postedby`),
  KEY `type` (`type`,`ontime`,`date`),
  KEY `order_id` (`order_id`,`type`,`uniqueid`),
  KEY `lookup order` (`order_id`,`ticket_id`,`visible`,`date`),
  KEY `ticket_id` (`ticket_id`),
  KEY `visible` (`visible`)
) ENGINE=InnoDB AUTO_INCREMENT=5907756 DEFAULT CHARSET=utf8
Thanks
shoryuken is offline   Reply With Quote