开发者

Mysql char search vs integer

开发者 https://www.devze.com 2023-04-09 00:55 出处:网络
I have this开发者_如何学编程 query: SELECT * FROM table WHERE field = \'abcd\'; Table has lots of rows (1\'000\'000\'000 or more)

I have this开发者_如何学编程 query:

SELECT * FROM table WHERE field = 'abcd';

Table has lots of rows (1'000'000'000 or more)

If instead of field = 'abcd' i use something like

SELECT * FROM table WHERE field = 'abcd' AND other_field = 300;

, Where 300 is a number that depends on 'abcd' (f(abcd) => 300), will the query run faster?


Search time when using unique indexes
MySQL uses BTree indexes.
This means that a unique row is found in log(n) time. If you search in 65536 rows, it will take max 16 comparisons to find the row, assuming a fully balanced tree.
If you search in 1,000,000,000 it will take 30 comparisons to find the row, assuming a fully balanced tree and unique values.

If you search on a unique key, it makes very little sense to add extra tests.

Search time using non-unique indexes
If you search on a non-unique tree, the search time will be fast, but of course the resultset will include more than 1 row, and the query time goes up as the number of rows grows. Provided that MySQL deems it useful to use an index, the initial key will be found in very few tests.

However the real time is spend going though the rows that match.

0

精彩评论

暂无评论...
验证码 换一张
取 消