我有一个在网站上运行的这样的查询:
SELECT *
FROM `orders`
WHERE
`co_id`='12660' &&
((`from_region`=6 && `to_region`=60) || `id` IN('539110')) &&
(
`p_name` LIKE '%search%' ||
`p_street_address` LIKE '%search%' ||
`p_city` LIKE '%search%' ||
`p_state` LIKE '%search%' ||
`p_zip` LIKE '%search%' ||
`p_non_us_address` LIKE '%search%' ||
`p_contact` LIKE '%search%'
) &&
(
(`when_deleted` >= 1349222400 && `when_deleted` <= 1378511999) ||
(`assigned_to_date` >= 1349222400 && `assigned_to_date` <= 1378511999) ||
(`deleted` = "0" && `assigned_to` = "")
)
ORDER BY p_state,p_city,p_zip,date_time DESC LIMIT 20,20;
这是一个搜索查询,可以在很多列中搜索文本。该表包含订单,查询仅检查特定地区的订单以及是否在特定日期之间(在此期间被删除或分配,或者仍然未完成)以及搜索文本。
表本身如下所示:
CREATE TABLE IF NOT EXISTS `orders` (
`co_id` smallint(5) unsigned NOT NULL DEFAULT '0',
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`from_region` smallint(5) unsigned NOT NULL DEFAULT '0',
`to_region` smallint(5) unsigned NOT NULL DEFAULT '0',
`deleted` tinyint(1) unsigned NOT NULL DEFAULT '0',
`when_deleted` int(10) unsigned NOT NULL DEFAULT '0',
`important` smallint(1) unsigned NOT NULL DEFAULT '0',
`p_name` varchar(50) NOT NULL DEFAULT '',
`p_street_address` varchar(80) NOT NULL DEFAULT '',
`p_city` varchar(50) NOT NULL DEFAULT '',
`p_state` char(2) NOT NULL DEFAULT '',
`p_zip` varchar(10) NOT NULL DEFAULT '',
`p_non_us_address` varchar(255) NOT NULL,
`p_country` varchar(15) NOT NULL,
`p_residence` tinyint(1) NOT NULL DEFAULT '0',
`p_contact` varchar(50) NOT NULL DEFAULT '',
`p_phone` varchar(25) NOT NULL,
`p_cell` varchar(25) NOT NULL,
`p_altphone` varchar(25) NOT NULL,
`p_fax` varchar(80) NOT NULL DEFAULT '',
`p_email` varchar(80) NOT NULL DEFAULT '',
`p_addl_info_1` varchar(255) NOT NULL,
`p_addl_info_2` varchar(255) NOT NULL,
`vehs_avail` int(10) unsigned NOT NULL DEFAULT '0',
`d_name` varchar(50) NOT NULL DEFAULT '',
`d_street_address` varchar(80) NOT NULL DEFAULT '',
`d_city` varchar(50) NOT NULL DEFAULT '',
`d_state` char(2) NOT NULL DEFAULT '',
`d_zip` varchar(10) NOT NULL DEFAULT '',
`d_non_us_address` varchar(255) NOT NULL,
`d_country` varchar(15) NOT NULL,
`d_residence` tinyint(1) NOT NULL DEFAULT '0',
`d_contact` varchar(50) NOT NULL DEFAULT '',
`d_phone` varchar(25) NOT NULL,
`d_cell` varchar(25) NOT NULL,
`d_altphone` varchar(25) NOT NULL,
`d_fax` varchar(80) NOT NULL DEFAULT '',
`d_email` varchar(80) NOT NULL DEFAULT '',
`d_addl_info_1` varchar(255) NOT NULL,
`d_addl_info_2` varchar(255) NOT NULL,
`deliver_by` int(10) unsigned NOT NULL DEFAULT '0',
`b_CustomerId` int(10) unsigned NOT NULL,
`b_name` varchar(50) NOT NULL DEFAULT '',
`b_street_address` varchar(80) NOT NULL DEFAULT '',
`b_city` varchar(50) NOT NULL DEFAULT '',
`b_state` char(2) NOT NULL DEFAULT '',
`b_zip` varchar(10) NOT NULL DEFAULT '',
`b_non_us_address` varchar(255) NOT NULL,
`b_country` varchar(15) NOT NULL,
`b_contact` varchar(50) NOT NULL DEFAULT '',
`b_phone` varchar(25) NOT NULL,
`b_cell` varchar(25) NOT NULL,
`b_altphone` varchar(25) NOT NULL,
`b_fax` varchar(80) NOT NULL DEFAULT '',
`b_email` varchar(80) NOT NULL DEFAULT '',
`b_addl_info_1` varchar(255) NOT NULL,
`b_addl_info_2` varchar(255) NOT NULL,
`num_units` decimal(5,2) unsigned NOT NULL DEFAULT '0.00',
`price_per_unit` decimal(9,2) unsigned NOT NULL DEFAULT '0.00',
`price_per_load` decimal(9,2) unsigned NOT NULL DEFAULT '0.00',
`additional_charge` decimal(9,2) NOT NULL DEFAULT '0.00',
`additional_charge_desc` text NOT NULL,
`fuelSurchargePercent` float(5,2) NOT NULL,
`fuelSurchargeAmt` decimal(9,2) unsigned NOT NULL,
`poNumber` varchar(50) NOT NULL,
`cod` tinyint(1) unsigned NOT NULL DEFAULT '0',
`run_drive` tinyint(1) NOT NULL DEFAULT '0',
`desc_vehs` text NOT NULL,
`other_notes` text NOT NULL,
`assigned_to` varchar(255) NOT NULL DEFAULT '',
`assigned_to_date` int(10) unsigned NOT NULL DEFAULT '0',
`assignedOn` datetime NOT NULL,
`loaded` int(10) unsigned NOT NULL DEFAULT '0',
`delivered` int(10) unsigned NOT NULL DEFAULT '0',
`notifications_entered` tinyint(1) unsigned NOT NULL DEFAULT '0',
`notifications_assigned` tinyint(1) unsigned NOT NULL DEFAULT '0',
`notifications_loaded` tinyint(1) unsigned NOT NULL DEFAULT '0',
`notifications_delivered` tinyint(1) unsigned NOT NULL DEFAULT '0',
`notifications_email` varchar(100) NOT NULL,
`owner` smallint(5) unsigned NOT NULL,
`date_time` int(10) unsigned NOT NULL DEFAULT '0',
`changed` tinyint(1) NOT NULL DEFAULT '0',
`synced` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`co_id`,`id`),
KEY `from_region` (`from_region`),
KEY `to_region` (`to_region`),
KEY `deleted` (`deleted`),
KEY `Delivered` (`delivered`),
KEY `owner` (`owner`),
KEY `b_CustomerId` (`b_CustomerId`),
KEY `co_id` (`co_id`),
KEY `co_id-p_name` (`co_id`,`p_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
我知道我可以在 p_name、p_street_address、p_city、p_state、p_zip、p_non_us_address 和 p_contact 上创建一些额外的索引,但这只是搜索上车地点。还有一个可以搜索的交付和计费位置。到我为所有这些插入添加索引时,速度会大大降低。是否有更好的方法可以构建此查询以提高其效率?分开查询?子查询?也许甚至有一种方法可以创建一个仅用于搜索的单独表?