Yazılı olarak, sorgu muhtemelen gerçek niyeti ihanet ediyor. İstenilen durum olduğunu olası görünüyor
WHERE ( ( toid='".$mid."' AND fromid='".$fid."' )
OR (toid='".$fid."' AND fromid='".$mid."')
)
AND subject != 'something'
But, as written, the query will only apply the subject condition with the second (toid and fromid) clause.
Note that in the above, the inner parenthesis are extraneous; never the less it is often a good idea to include them to show the intended expression more explicitly.
Her iki durumda da, this query is a "hard[er]" query to resolve, OR tümcesi ve bir EŞİT DEĞİL yüklem nedeniyle. (Diğer stratejiler mümkün olmasına rağmen) OR yan tümcesi, genellikle sunucu iki alt sorgu sonuçlarını birleştirme neden olur. EŞİT DEĞİL yüklemi bir dizin arama) tarafından eldeki satır yüklemi karşılayan olmadığını değerlendirmek için ana tablo / diğer indekslere gezi kaydeder için, (ancak bir kaplama indeks bazı durumlarda yardımcı olur) çözülemez
Bağımsız olarak bu mümkün mantıksal sorunu, dizinleri ekleme birden fazla anahtar durum yardımcı olacaktır olacaktır. Ben şu önermek istiyorum:
- romatizmal, fromId, konu
- romatizmal, fromid
Ayrıca konuyu içeren endeksinin faiz sorgu yerine konuyu arama zorunda daha endeksinin kısmi bir tarama ile çözülecek sağlamaktır. Bu indeks bu sorgu için bir kaplama indeksi olarak kullanılacaktır.
Dizinleri ekleme INSERT, UPDATE ve DELETE işlemleri için performans azalır, ancak dikkat edin.
Edit: on the usability of the (toid,fromid, subject) index
First off, it is acknowledged that we need only one of the suggested indexes, i.e. if we have the (romatizmal, fromid, subject) index, the (romatizmal, fromid) one would be redundant (albeit possibly more efficient if subject was a relatively long column).
This said, the fact that the query uses a NOT EQUAL predicate on subject doesn't necessary exclude the use of the subject data in the (romatizmal, fromid, subject) index. The reason for that is that the [not equal] condition on subject can be resolved within the index (not requiring a match/merge or a lookup, i.e. akin to some "covering" logic)