S
S
serikd2016-04-08 13:08:58
SQL
serikd, 2016-04-08 13:08:58

Isn't it considered a sign of bad SQL query architecture?

Hello.
Is the following SQL valid and is it a sign of bad application architecture?
The task is to display posts that either do not have an end date set (unlimited), or the date has not yet arrived (temporary)

.. AND ('end_time' >= NOW() OR 'end_time' IS NULL) ..

Maybe when adding, fill end_time + 100 (200) years?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2016-04-08
@AlexListen

IS NULL is not very good. As far as I remember, there are problems with indexes if there are NULL in the rows, it is better to declare such columns with default limit values, such as 9999-12-31 23:59:59, and to be very good, then this ... AND `end_time` BEETWEN ( NOW(), '9999-12-31 23:59:59')

S
sim3x, 2016-04-08
@sim3x

...AND ( 'is_public' == 1 OR 'end_time' >= NOW() )

E
Evgeny Bykov, 2016-04-09
@bizon2000

Actually, from the point of view of architecture, it is correct to use NULL for missing values ​​- this value exists for that.
As for indexes - it depends, firstly, on which RDBMS you are using, and secondly, on whether the index contains other columns.
So, for example, in Oracle, keys consisting of only NULLs do not get into the index, i.e., if, for example, the index is built on 2 columns: id and start_date, then this key will not get into the index, only if both of these columns contain NULL .
At the same time, in MS SQL Server, NULL values ​​are indexed in the same way as other values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question