I have a table that saves the needed information to send notifications to users. now the UID columns in this table is almost unique because mostly each user subscribes with one device. but it is possible to have multiple rows with same UID so unique index is out of picture and because of almost unique nature of data i don't think usual index be useful either as it's going to take almost same time to search index as whole table.

Currently the solution that comes to my mind is saving primary code of each row of notifications table in a column in Users table in comma separated format.


An index doesn't need to be unique to be efficient, and in fact I've rarely found making my indexes unique made much of a difference in terms of performance, in my experience. (It does in certain cases, data size dependent, but not enough to be measurably important to me so far.) On that note, my experience is within Microsoft SQL Server, so knowing which database system, version, and implementation (when applicable) would be helpful here.

That said, in most modern Relational Database Management Systems (RDBMS), the standard index is a B-Tree index. That means your data is stored in a B-Tree logical data structure which is much more efficient for searching than the entire unindexed Table itself. It doesn't matter if your data is "almost" unique vs perfectly unique. If your data was completely redundant (every value for the column(s) you anticipate indexing are exactly the same) then indexing probably won't help you much, but in your case a regular B-Tree index sounds like it would work very good.

  • Thanks for your response. i'm using mysql. Mar 22 at 18:24
  • 1
    @PooyaEstakhri No problem! And yes, MySQL supports standard B-Tree indexes. B-Tree indexes, so the more unique your data is the better they are, but regardless they'll be better than an unindexed Table anytime, when your data is almost perfectly unique anyway.
    – J.D.
    Mar 22 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.