indexing - index a bit field in MYSQL -
updated question:
suppose data i'm interested in field=1 , actual ratio of data wheere field 1 vs 0 small(eg. 1%) in case, index field benefit select field =1 query?
original question:
have int field have either 0 or 1 value, indexing field speed select queries such as:
select * xxx field=1;
generally speaking, no. bi-state field doesn't speed queries when indexed because have @ half rows on average. want index entries selective - given entry in index should represent small percentage of possible values (say, less 10%, preferably fractions of percent). using index ignores of data in table, gives performance benefit.
some dbms support bitmap indexes. can help, still run problem of selectivity.
the updated question says number of values value 1 small (less 1 percent); index give benefit now?
the answer is:
for queries specify value 1, yes, index on column provide benefit, provided optimizer makes use of index. may need tweak dbms make realize index skewed in favour of using queries value 1; tends dbms-specific, updating statistics in various guises name of game, possibly using hints in sql queries too. of course, if optimizer never uses index, still provides no benefit - , optimizer may decide other indexes more in way.
for queries value 0, index should not used. chances are, though, the dbms continue maintain index 0 values - though should never use them. unusual dbms commanded 'only index column values other zero', though beneficial.
so - depends. depends on queries, , depends on optimizer.
note composite index - on other customarily used columns , bit-field may provide benefit. so, if select on date range, composite index on date , bit-field columns (probably in order) should provide index.
Comments
Post a Comment