日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 索引 lt =,当同时使用gt; =和lt; =时如何索引MySQL表?

發布時間:2023/12/15 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 索引 lt =,当同时使用gt; =和lt; =时如何索引MySQL表? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Let's say you have a query like,

select ID, REGION, START, END from COORD_SYSTEM

where REGION=? and TYPE=? and START >= ? and END <= ?;

And let's say this table has around 50,000 rows. The REGION column has 500 distinct values and the TYPE column has 50 distinct values. The ID column is the primary key.

What would be the best way to index the table? I'm not quite sure if one could achieve a covering index due to the >= and <= signs. Here are a few options:

create index on COORD_SYSTEM (REGION, TYPE)

create index on COORD_SYSTEM (REGION, TYPE, START)

create index on COORD_SYSTEM (REGION, TYPE, START, END)

Update - Here's the explain statement:

id: 1

select_type: SIMPLE

table: COORD_SYSTEM

type: range

possible_keys: indx_A

key: indx_A

key_len: 50

ref: NULL

rows: 590

Extra: Using where

1 row in set (0.00 sec)

解決方案

There's no reason that you can't use a covering index with range operators. The challenge (for non-covering indexes) is that the optimizer might think that a full scan might result in fewer page reads if your range is large, and the index won't be used for some of your queries. Similarly, for some parameter values, the optimizer may choose to do a full scan if the covering index isn't much better than a scan for some sets of parameters.

So, given the description in the question it's not actually possible to give the optimal solution for all cases.

What I tend to do with things like that is:

Create a copy of the database

Guess which index might do the job, and create that index.

EXPLAIN several queries with different sized ranges (more ranges need more I/O to get back to the table data if you haven't covered the query, so you should try for common range sizes and outliers)

Drop the index and try another, perhaps with a different covering index with the columns in a different order

You might even choose to create two or more covering indexes with the fields in different orders, assuming you run this query considerably more times than corresponding INSERTs or UPDATEs, and that the size of the index isn't a factor for disk space usage.

總結

以上是生活随笔為你收集整理的mysql 索引 lt =,当同时使用gt; =和lt; =时如何索引MySQL表?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。