$title = '%' . $_GET['term'] . '%';
$cpt1 = 'type1';
$cpt2 = 'typ2';
$sql = $wpdb->prepare("
(SELECT ID, post_title, post_type FROM wc_posts WHERE (post_title LIKE %s) AND (post_type = %s) LIMIT 5)
UNION
(SELECT ID, post_title, post_type FROM wc_posts WHERE (post_title LIKE %s) AND (post_type = %s) LIMIT 5)
ORDER BY post_type DESC, post_title", $title, $cpt1, $title, $cpt2);
$data = $wpdb->get_results( $sql );
*************** row 1 ***************
id: 1
select_type: PRIMARY
table: showme_posts
type: ref
possible_keys: type_status_date
key: type_status_date
key_len: 62
ref: const
rows: 62700
Extra: Using index condition; Using where
*************** row 2 ***************
id: 2
select_type: UNION
table: showme_posts
type: ref
possible_keys: type_status_date
key: type_status_date
key_len: 62
ref: const
rows: 234474
Extra: Using index condition; Using where
*************** row 3 ***************
id: NULL
select_type: UNION RESULT
table: <union1,2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Using temporary; Using filesort
CREATE TABLE `showme_posts` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext NOT NULL,
`post_title` text NOT NULL,
`post_excerpt` text NOT NULL,
`post_status` varchar(20) NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) NOT NULL DEFAULT 'open',
`ping_status` varchar(20) NOT NULL DEFAULT 'open',
`post_password` varchar(20) NOT NULL DEFAULT '',
`post_name` varchar(200) NOT NULL DEFAULT '',
`to_ping` text NOT NULL,
`pinged` text NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext NOT NULL,
`post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
KEY `post_parent` (`post_parent`),
KEY `post_author` (`post_author`)
) ENGINE=InnoDB AUTO_INCREMENT=522366 DEFAULT CHARSET=utf8
CREATE FULLTEXT INDEX post_type_post_title ON showme_posts (post_type, post_title)
-------------------------- запрос 3 ---------------------------------
InnoDB | MyISAM | MyISAM_FULLTEXT | MyISAM_FT_MATCH
6.0870 | 1.4575 | 1.4636 | 1.3553
-------------------------- запрос 2 ---------------------------------
InnoDB | MyISAM | MyISAM_FULLTEXT | MyISAM_FT_MATCH
6.0486 | 1.5553 | 1.5511 | 1.3559
-------------------------- запрос 1 ---------------------------------
InnoDB | MyISAM | MyISAM_FULLTEXT | MyISAM_FT_MATCH
3.4546 | 0.1335 | 0.1317 | 0.0005
SELECT ID, post_title, post_type FROM showme_posts WHERE
(MATCH (post_type,post_title) AGAINST ('%my_search_query%')) AND (post_type = 'type_1')
OR
(MATCH (post_type,post_title) AGAINST ('%my_search_query%')) AND (post_type = 'type_2')
LIMIT 5
get_posts()
В РАЗЫ быстрее запроса через new WP_Query()
с одними и теми же аргументами, хотя по идее разницы в их производительности быть не должно. Пойду дальше переписывать запросы)