Question

Difficult question to phrase, so let me explain.

As part of an RSS caching system I'm inserting a lot of rows into a DB, several times a day. One of the columns is 'snippet', for the description node in the RSS feeds.

Sometimes this node is far longer than I want, since the corresponding DB column is type "tiny text" (max: 255 chars).

So, in terms of computation/memory, is it better for me to truncate via PHP before insertion, or just feed the whole, too-long string to MySQL and have it do the truncation?

Both of course work, but I wondered if one was better practice than the other.

Was it helpful?

Solution

In cases like this it's probably best to measure. If you don't notice a difference then it doesn't matter.

My intuition tells me that, since your snippet size is very small and the plain text can be very big it would be better to truncate before hand. Take the performance hit in PHP so you don't spend a lot of time sending a large query to MySQL.

For readability and code clarity it would also be better to do the truncation in PHP because that makes it explicit. You can even do clever truncating by word or by sentence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top