देखें MySQL डॉक्स FORCE INDEX
के लिए कर रहे हैं।
JOIN survey_customer_similarity AS scs
FORCE INDEX (CONSUMER_ID_1,CONSUMER_ID_2)
ON
cr.CONSUMER_ID=scs.CONSUMER_ID_2
AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_1
OR cr.CONSUMER_ID=scs.CONSUMER_ID_1
AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_2
रूप TheScrumMeister नीचे बताया गया है, यह आपके डेटा पर निर्भर करता है, चाहे के इंडेक्स वास्तव में एक ही बार में इस्तेमाल किया जा सकता।
यहां एक उदाहरण दिया गया है जहां आपको क्वेरी निष्पादन और चौराहे को नियंत्रित करने के लिए तालिका को दो बार प्रकट करने की आवश्यकता है। इस फिल्टर
i in (2,3)
और 1K
j in (2,3)
मिलान पंक्तियाँ मिलान मोटे तौर पर 1K पंक्तियों के साथ> 100K रिकॉर्ड के साथ एक तालिका बनाने
उपयोग करने के लिए,:
drop table if exists t1;
create table t1 (id int auto_increment primary key, i int, j int);
create index ix_t1_on_i on t1(i);
create index ix_t1_on_j on t1(j);
insert into t1 (i,j) values (2,2),(2,3),(4,5),(6,6),(2,6),(2,7),(3,2);
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i*2, j*2+i from t1;
insert into t1 (i,j) select i, j from t1;
insert into t1 (i,j) select i, j from t1;
insert into t1 (i,j) select 2, j from t1 where not j in (2,3) limit 1000;
insert into t1 (i,j) select i, 3 from t1 where not i in (2,3) limit 1000;
जब कर रही:
select t.* from t1 as t where t.i=2 and t.j=3 or t.i=3 and t.j=2
आप वास्तव में प्राप्त 8 मैचों:
+-------+------+------+
| id | i | j |
+-------+------+------+
| 7 | 3 | 2 |
| 28679 | 3 | 2 |
| 57351 | 3 | 2 |
| 86023 | 3 | 2 |
| 2 | 2 | 3 |
| 28674 | 2 | 3 |
| 57346 | 2 | 3 |
| 86018 | 2 | 3 |
+-------+------+------+
का उपयोग करें क्वेरी पर 0 से ऊपर पाने के लिए:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | t | range | ix_t1_on_i,ix_t1_on_j | ix_t1_on_j | 5 | NULL | 1012 | Using where
यहां तक कि अगर हम एक के इंडेक्स EXPLAIN
पर सवाल के FORCE INDEX
जोड़ने सटीक एक ही बात वापस आ जाएगी।
यह इंडेक्स भर में इकट्ठा कर, और फिर उन्हें एक दूसरे को काटना करने के लिए, इस का उपयोग करें: प्राप्त करने के लिए
select t.* from t1 as a force index(ix_t1_on_i)
join t1 as b force index(ix_t1_on_j) on a.id=b.id
where a.i=2 and b.j=3 or a.i=3 and b.j=2
उपयोग कि explain
साथ क्वेरी:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | a | range | ix_t1_on_i | ix_t1_on_i | 5 | NULL | 1019 | Using where
1 | SIMPLE | b | range | ix_t1_on_j | ix_t1_on_j | 5 | NULL | 1012 | Using where; Using index
यह साबित होता है कि अनुक्रमित किया जा रहा है । लेकिन यह कई अन्य कारकों के आधार पर तेजी से हो सकता है या नहीं भी हो सकता है।
कृपया MySQL संस्करण, तालिका परिभाषाएं और EXPLAIN आउटपुट पोस्ट करें। –
इसके अतिरिक्त, क्या आप पूरी क्वेरी ... या कम से कम बाकी को दिखा सकते हैं जो शायद "गोपनीय" पर विचार न करें ... व्हाटटेबल से एक्स का चयन करें ... शामिल हों ... कहां ... समूह द्वारा। .. – DRapp