Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dedicated SQL table containing only unique strings

I can't seem to find any examples of anyone doing this on the web, so am wondering if maybe there's a reason for that (or maybe I haven't used the right search terms). There might even already be a term for this that I'm unaware of?

To save on database storage space for regularly reoccurring strings, I'm thinking of creating a MySQL table called unique_string. It would only have two columns:

  1. id : INT : PRIMARY_KEY index
  2. string : varchar(255) : UNIQUE index

Any other tables anywhere in the database can then use INT columns instead of VARCHAR columns. For example a varchar field called browser would instead be an INT field called browser_unique_string_id.

I would not use this for anything where performance matters. In this case I'm using it to track details of every single page request (logging web stats) and an "audit trial" of user actions on intranets, but other things potentially too.

I'm also aware the SELECT queries would be complex, so I'm not worried about that. I'll most likely write some code to generate the queries to return the "real" string data.

Thoughts? I feel like I might be overlooking something obvious here.

Thanks!

like image 680
LaVache Avatar asked Aug 05 '26 10:08

LaVache


1 Answers

I have used this structure for a similar application -- keeping track of URIs for web logs. In this case, the database was Oracle.

The performance issues are not minimal. As the database grows, there are tens of millions of URIs. So, just identifying the right string during an INSERT is challenging. We handled this by building most of the update logic in hadoop, so the database table was, in essence, just a copy of a hadoop table.

In a regular database, you would get around this by building an index, as you suggest in your question. And, an index solution would work well up to your available memory. In fact, this is a rather degenerate case for an index, because you really only need the index and not the underlying table. I do not know if mysql or SQL Server recognizes this, although columnar databases (such as Vertica) should.

SQL Server has another option. If you declare the string as VARCHAR(max), then it is stored on a separate data page from the rest of the data. During a full table scan, there is no need to load the additional page in memory, if the column is not being referenced in the query.

like image 70
Gordon Linoff Avatar answered Aug 07 '26 23:08

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!