I have a table that caches data (shared hosting so no memcached) to a MySQL table.
The concept is this:
I have a page that loads (static) data and then cache then:
- If the cache does not exist then it queries the page then render the HTML and save it to the cache table.
- If a page does not exist in cache, it executes 12 queries (menu, page content, SEO, product list, etc.) then saves the rendered HTML in the table.
The cache table is like this:
=cache=
url varchar(255) - primary key
page mediumtext
Now I think I’m doing the right thing, based on what I have (shared host, no caching like memcached, etc.) but my question is this:
Because the URL is a varchar index but because numeric IDs (like int) are faster, is there a way to convert a URL like /contact-us/ or /product-category/product-name/ to a unique integer? Or is there any other way to optimize this?
I would create some form of hash which would allow a shorter key. In many cases something simple like a hash of the request path may be viable. Alternatively something even simpler like CRC32(‘/your/path/here’) may be suitable in your situation as a primary key. In this example the following columns would exist
You could then take this a step further, and add trigger
BEFORE INSERTwhich would calculate the value for urlCRC, i.e. containingYou could then create a stored procedure which takes as argument inURL (string), and internally it would do
If the number of rows returned is 0, then you can trigger logic to cache it.
This may of course be overkill, but would provide you a numeric key to work on which, assuming there are no conflicts, would suffice. By storing the url as VARCHAR(255) also then if a conflict does occur, you can easily regenerate new hashes using a different algorithm.
Just to be clear, I just use CRC32() as an example off the top of my head, chances are there are more suitable algorithms. The main point to take away is that a numeric key is more efficient to search so if you can convert your strings into unique numerics it would be more efficient when retrieving data.