Search Postgresql Archives

Re: Is there a better way to do this?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Aug 28, 2007, at 17:22 , D. Dante Lorenso wrote:

Michael Glaesemann wrote:
On Aug 28, 2007, at 16:55 , D. Dante Lorenso wrote:
    RETURN time + (days || ' days')::INTERVAL;
It's bad practice to concatenate like this. Use time + days * interval '1 day' and be done with it.

Why? Is this functionality expected to break in the future or has unexpected side effects? Is it less clear or less efficient? Who declared it bad practice and where can I read that documentation?

It's generally bad practice to interpolate unnecessarily. You're right, in this case you're probably safe from this particular case ever changing. I personally find it less clear (though clarity is often in the eye of the beholder). time + * interval '1 day' is to me a clearer expression of what you're doing: add this multiple of days to the time.

(days || ' days')::interval says "Make a string using this value (oh, it's an int? we need to cast it to text) and this string: the result just happens to match the proper input format for an interval, which is fortunate because now we're casting the string to interval". Okay, there's a little editorializing thrown in, but that's what the concatenation says to me.

The concatenation is probably less efficient: you're casting an int to text and then the text to interval with the concatenation you're using. I don't know how that compares in terms of cycles to the int * interval math, but efficiency isn't really the reason I would avoid it.

I'm sure others could provide more cogent explanations, but those are my initial thoughts.

Michael Glaesemann
grzm seespotcode net



---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux