I understand that there are many Order By Questions with various solutions, but what I ask depends on the data structure that I have installed below.
I have the following two tables installed:
Table setting
CREATE TABLE `record` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`file_group_id` INT(11) NOT NULL,
`status_id` INT(11) NOT NULL,
`title` VARCHAR(3000) NOT NULL,
`date_created` DATETIME NOT NULL,
`user_created` INT(11) NOT NULL,
`publish_date` DATETIME NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_RECORD_FGID` (`file_group_id`)
);
CREATE TABLE `record_meta_text` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`record_id` INT(11) NOT NULL,
`column_id` INT(11) NOT NULL,
`value` VARCHAR(3000) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_RMT_VALUE` (`value`(800)),
KEY `IDX_RMT_COL_ID` (`column_id`),
KEY `IDX_RMT_RECORD_ID_COL_ID` (`record_id`,`column_id`,`value`(800))
)
Request without order
SELECT
r.title AS col_title,
rmt_2780.value AS col_2780,
rmt_2781.value AS col_2781,
rmt_18474.value AS col_18474
FROM
record r
INNER JOIN record_meta_text AS rmt_2780 ON rmt_2780.record_id = r.id AND rmt_2780.column_id = 2780
INNER JOIN record_meta_text AS rmt_2781 ON rmt_2781.record_id = r.id AND rmt_2781.column_id = 2781
INNER JOIN record_meta_text AS rmt_18474 ON rmt_18474.record_id = r.id AND rmt_18474.column_id = 18474
WHERE
r.file_group_id = 2350
AND r.status_id = 1
LIMIT 0, 50
Conclusion and explanation
Lead time: .004 seconds
Here is the output of EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra
------ ----------- --------- ------ --------------------------------------- ------------------------ ------- ---------------------------- ------ -------------
1 SIMPLE r ref PRIMARY,IDX_RECORD_FGID IDX_RECORD_FGID 4 const 527895 Using where
1 SIMPLE rmt_18474 ref IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID IDX_RMT_RECORD_ID_COL_ID 8 file_cabinet_data.r.id,const 1 (NULL)
1 SIMPLE rmt_2780 ref IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID IDX_RMT_RECORD_ID_COL_ID 8 file_cabinet_data.r.id,const 1 (NULL)
1 SIMPLE rmt_2781 ref IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID IDX_RMT_RECORD_ID_COL_ID 8 file_cabinet_data.r.id,const 1 (NULL)
Request for order
SELECT
r.title AS col_title,
rmt_2780.value AS col_2780,
rmt_2781.value AS col_2781,
rmt_18474.value AS col_18474
FROM
record r
INNER JOIN record_meta_text AS rmt_2780 ON rmt_2780.record_id = r.id AND rmt_2780.column_id = 2780
INNER JOIN record_meta_text AS rmt_2781 ON rmt_2781.record_id = r.id AND rmt_2781.column_id = 2781
INNER JOIN record_meta_text AS rmt_18474 ON rmt_18474.record_id = r.id AND rmt_18474.column_id = 18474
WHERE
r.file_group_id = 2350
AND r.status_id = 1
ORDER BY col_2780
LIMIT 0, 50
Conclusion and explanation
Lead time: 35.237 seconds
id select_type table type possible_keys key key_len ref rows Extra
------ ----------- --------- ------ --------------------------------------- ------------------------ ------- ---------------------------- ------ ----------------------------------------------
1 SIMPLE r ref PRIMARY,IDX_RECORD_FGID IDX_RECORD_FGID 4 const 527895 Using where; Using temporary; Using filesort
1 SIMPLE rmt_2780 ref IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID IDX_RMT_RECORD_ID_COL_ID 8 file_cabinet_data.r.id,const 1 (NULL)
1 SIMPLE rmt_2781 ref IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID IDX_RMT_RECORD_ID_COL_ID 8 file_cabinet_data.r.id,const 1 (NULL)
1 SIMPLE rmt_18474 ref IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID IDX_RMT_RECORD_ID_COL_ID 8 file_cabinet_data.r.id,const 1 (NULL)
Question and solution
So my question is how can I make this not stay forever with order. In several cases I will have multiple orders according to the conditions, as well as where the statements are. LIMIT / OFFSET is for swapping.
The table recordcurrently has 1,139,119 entries.
The table file_meta_textcurrently contains 7,584,428 entries.
SQLFiddle , ( ):
http://sqlfiddle.com/#!2/6ffc3/1
.