NGSI-LD time range query - fiware

I tried doing a query to get entities (/entities?q=...) in a specific time range based on a DateTime attribute but I didn't have any luck. I read the NGSI-LD specification on the queries but I couldn't come up with the right solution.

Related

Date vs Strings in MySQL [duplicate]

I have to allow my users to make an update with 24 hours of delay from their previous update, so because date operations in this case are going to be on the backend, but in the web server, i can store Javascript Date as a string in the database and when i have to calculate the difference between 2 dates i can query the lastUpdateDate from the database and parse it to a JS Date Object, is this safe to do?
Is it safe to store dates as a string in mysql?
It is safe as long as the format that you use to represent your dates is unambiguous (that is, each value maps to a unique date).
But it is always inefficient not to use the proper datatype to store a value. Sooner or later, you will face the need to do some date computation in the database (sorting, filtering, adding, ...): storing your dates as strings will make such operation more complex that it has to (the overhead varies depending on the format your choose), and much less efficient (you would typically need to translate all the strings to dates before you can operate on them).
On the other hand, using the proper datatype from the start does not makes things more complicated on the frontend - especially in MySQL. You just need to format your strings properly ('YYYY-MM-DD HH:MI:SS') before passing them to the database, and MySQL will happily treat them as dates.
It is, unless your date strings are unique. You can specify UNIQUE clause for it. But there is no reason to do so, because MySql provides 5 built-in Date/Time data types(DATE, DATETIME, TIMESTAMP, TIME, YEAR(M)). Still if you save dates as a string, you might face problems later in extracting dates in right format.
I usually store date and time in both formats, as a string and in the native database format. Despite the disadvantages of some extra processing and extra storage space, I find it useful because all my search and reporting queries are based on the text format. This makes my application database-agnostic since text comparisons are the same across all major databases, whereas date and time formats vary significantly across different databases. I use the native database format stored dates and times for date-based calculations.

Storing date as integer

I came across a database schema (instant messaging, http://www.9lessons.info/2013/05/message-conversation-database-design.html) where message's date + time is stored not as timestamp, but with an integer, like 123984347439.
What's the point of this?
I found a couple of resources which store dates as integers, like 20151009. What are pros and cons of this approach in comparison native date + time specific formats of databases?
When stored as integer, the timestamp is not reliant on any time zone settings of the server. When you send a date to MySQL server, it will try to convert it to UTC for storage, if the column type is timestamp. It will perform the same conversion when it pulls the date out.
You can read about it in the manual, 5th paragraph.
MySQL converts TIMESTAMP values from the current time zone to UTC for
storage, and back from UTC to the current time zone for retrieval.
(This does not occur for other types such as DATETIME.) By default,
the current time zone for each connection is the server's time.
With integer saved, this doesn't happen.
Pros:
you are not reliant on the server's time zone
Cons:
you can't use date functions easily, without performing conversions first using FROM_UNIXTIME()
when reading the data manually, numbers don't tell you what date is in question. The timestamp column formats it so you can understand the date without problems
Update: I don't know what's the benefit of storing an integer that isn't unix timestamp. Storing the date such as 20151009 corresponds to 10.09.2015 - I don't see any kind of use for this without further information so my personal opinion boils down to that the person who designed such a system either didn't know much about dates and handling them or there's some kind of awkward business logic in the app itself that requires dates formatted like that in order to work. Bottom line is that I personally wouldn't use it. I'd stick to proven standards that work for everyone.
From the post that you linked it appears that the row is storing a Unix timestamp.
This allows you store the value as an integer while also allowing for method calls to retrieve as human readable date using from_unixtime() So if need be you can call SELECT from_unixtime(time) ASdateFROM conversation where user_id_fk = '3' and retrieve a value like 2007-11-30 10:30:19 from the result.
To answer your question about pros and cons, the biggest pro for storing as an integer is that comparing integers is a lot faster than datetimes. However, when stored as a Unix timestamp, there are some caveats(cons). Integers are factually faster when comparing unix_time = '1106475000' vs date_field = '2005-01-23 10:10:00; However, the problem arises when you need to compare an integer(Unix timestamp) to a human readable timestamp. Because you will need to convert the value to a Unix timestamp ahead of time in code or in the query, it will take some extra resources, or in case of in query it is much slower than native date comparison. So now this int comparison unix_time = UNIX_TIMESTAMP('2005-01-23 10:10:00') is a lot slower than date_field = '2005-01-23 10:10:00.
So it really depends on how your server side code is done to either leverage the speed of storing as an integer. As well it is up to the developer to decide if this speed is worth the extra abstraction between the sql server and the application.
Here is some more information on date/integer comparisons in innodb.
Here is some more information on date/integer comparisons in myisam.
That is most likely a timestamp expressed in seconds from a chosen epoch (starting date and time). There are a few standard timestamps out there, such as Unix timestamps
Storing dates as integers may speed up date calculations / comparisons in certain simple cases, since neither mysql, nor the processing application need to convert the underlying data to a date format. It also saves some space on your HD. The drawback is that you can't use the built-in date management functions. So, if you want to perform complex calculations, then either you write your own functions to do those or you need to convert the integer back to a date format.
The benefit is when you dont actually need the date or time - such as when you are storing peoples birthdates and are just using them as a matching value - not actually using them as dates and you dont want to store strings.

ORDER BY date bugs

I know there's some of those questions on SO already, but I can't find the correct answer.
I'm trying to ORDER by a date that is formatet like this: 01-01-1999. My query looks like this:
SELECT id, header, date FROM table ORDER BY date DESC
I'm getting 4 rows which seems okay, (They are the same) but then I get some odd results further on.
The fields are VARCHAR(); - what field should it be to store 01-01-1999 formats in?
The odd results is that I'm getting 4x30-08-2012.. And then lots of old dates, but then suddenly there's 2 records with 13-09-2012.
My question is: How come it's not sorting them: 2x13-09-2012 and then 4x30-08-2012 and then further on.
UPDATE
The problem is solved. But still: What data-field would I use for my dates instead of VARCHAR?
juergen has given an answer which basically involves the date being parsed each time. Two other alternatives suggest themselves though:
Change your database schema so that the column is already a date type (e.g. DATE) rather than a string. Life is often a lot better when you make your database columns accurately represent the type of data you want to store in them.
If you really need to store the data in string columns, consider changing the format to yyyy-MM-dd. That's a naturally sortable format, in that the "alphabetic" ordering is compatible with the "semantic" ordering of dates.
The first option is definitely preferrable IMO. This goes way beyond ordering, to:
Validation (well, hopefully; I gather MySQL is rather more forgiving of invalid data than I personally like, but...)
Conversion: I'm not sure what the PHP support is like, but in general you should be able to send and receive date/time values to/from the database without converting them into strings, which removes error-prone conversions
Clarity of purpose: give us much help as possible to anyone examining the database. Whatever you know about the data, put it into the schema. If you store everything as varchar regardless of whether it's text, numeric or date/time data, you might as well say "Well, it's just some stuff"
try
SELECT id, header, date FROM table
ORDER BY str_to_date(date, '%d-%m-%Y') DESC
See STR_TO_DATE
The best way to store dates in strings is 'YYYY-MM-DD'. MySQL has native format for dates - DATETIME.

Is it a good idea to use string data type for dates in MySQL instead of using datetime data type?

While implementing web applications on top of MySQL database, I'm thinking if it is a good idea to just use string data type to store dates?
For example, I can just store dates as '201110191503999' into database. Also this is convenient to query by date. For example, select * from some_table where the_date like '20111019%'
Is there any performance issue if we use string for dates? and are there any advantages for using date/datetime data type?
Thanks in advance!
Always use the column type for what what is needed; if you are using a date, use DATETIME, if it is a timestamp, use TIMESTAMP and so on.
Depending on in what you are coding, all the formatting of the data can be done on the actual page in whatever language you are using.
Also, you can take advantage of MySQL functions such as NOW(), rather than using the language's version and then storing it into the database.
If your data is of date type then store the data in a DATE (or DATETIME if you have a time element to it).
If you store dates as strings then you are asking for trouble! For example, what is to stop somebody writing a value of 'I am not a date' into you string 'date' field? Or what happens if you have '20111019' and '2011-10-19' and want them to be treated as equal? Furthermore you will be missing out on a whole raft of MySQL DATE and TIME specific functions
Never store a date as a string if you can possibly avoid it.
I am thinking of doing the same thing. I have been wrestling with MySQL trying to get it to store a timezone independent value in the database - something based off GMT. It is really not working. Tried all kinds of flags useTimeZone=true and JDBCShift with Java - still not getting any liftoff. Also Kuala Lampur timezone does not work because of some exotic Java message. So, if you can control the format, sure, use a String type. Many have done it before.

Best practice for storing the date in MySQL from PHP

I've been using the unix timestamp all my life.
I like it because it's easy to compare, it's fast because I store it as an integer. And since I'm using PHP, I can get any date/time format with date() function from the unixtimestamp.
Now, some people are saying that it's best to use the DATETIME format. But besides the more suited name, I don't see any advantages.
Is it indeed better to use DATETIME, if so, what are the advantages?
Thanks.
If you store dates as Unix timestamps in the database, you're giving yourself the heavy lifting. You have to convert them to the formats you want to use, you have to do the calculations between date ranges, you have to build the queries to get data in a range. This seems counter-intuitive- surely your "programmer time" is best spent solving real problems?
It seems much better practice to store dates and times in the proper format that MySQL has available, then use the database functions to create the queries for the data you want. The time you would waste doing all the convertions and mucking about is massive compared to the afternoon spent reading (and understanding) 11.6 MySQL Date and Time Functions
I've also been a huge fan of the unix timestamp all my life. But I think the correct answer is: "depends". I recently did a single table database where I wanted to only list URLs. There would be a date field, but the date field is purely for sorting. I.e order by last_crawled. Which means I will never use any built-in date functions on that field. It is merely an easy way to get the oldest entries first and I will never apply date functions to this field. Now, had I made this a date field, I would have lost out on two things:
A datetime field is twice the size of an integer
Sorting by an integer is faster (not 100% sure of this, pending outcome of this question)
However, for another system I had to store transactional information. This made using internal mysql date functions possible which turned out to be very useful when we had to start doing reports.
One advantage of using the MySQL date/time types is to be able to more simply use the date/time functions in MySQL.
The DATE type also has the advantage in that its only storing day, month and year so there is no space wasted or comparison complication that a seconds since epoch time would have for situations where you only cared about the day and not the time.
Personally I tend to use a database as just a dump for data so such functions are of little interest. In PHP I tend to just store the date in integer format for pretty much the reasons you state.
#Smita V, the inefficient query to which you refer is only so because you're applying your conversion function incorrectly to every table row, where you should apply it to the condition itself. So instead of
select col1,col2,colUnixdatetime from table where From_Unixtime(colUnixdatetime) between wtvdate1 and wtvdate2
, which converts every row on the table to compare it to the date you've got. You should use
select col1,col2,colUnixdatetime from table where colUnixdatetime between UNIX_TIMESTAMP(wtvdate1) and UNIX_TIMESTAMP(wtvdate2).
Doing it this way WILL use the appropriate table indexes.
#treznik a while ago I moved from a uts integer to a datetime or timestamp data types, for the reasons mentioned above, in that they're much easier to read and manipulate (I do quite a lot of direct table access). However I've lately started to re-think this approach for two reasons:
There is no time zone location stored, so you're inferring the time zone based on your location. This may or may not be an issue for you.
It ignores daylight saving time. So when the clocks go back at 2am, you will get 1:30am twice, and saying 2011-10-30 01:30 doesn't let you know this, whereas 1319938200 does. I don't think there's a native way in mysql to store date including time zone, except as a string (2011-10-30 01:30 BST).
I'm still trying to figure out the answer to this, myself.
Using database datetime is more efficient because every time you need to query you would need to apply from_unixtime() function to extract data from unix datetime col of the table. Using this function in where clause will completely ignore any index usage.
say my query is:
select col1,col2,colUnixdatetime from table where colUnixdatetime between wtvdate1 and wtvdate2
I would need to run:
select col1,col2,colUnixdatetime from table where From_Unixtime(colUnixdatetime) between wtvdate1 and wtvdate2
This above query will completely ignore any indexes, well there is no use of indexes here as they will never be used coz I will always have to use a function to get the real date time.
Any in-built function used on LHS of condition in a where clause would not use any indexes and if you have a HUGE table, your query will take longer.
Easier maintenance is a plus. Seeing the actual date when you do:
select * from table where ...
is pretty nice.
Easier to compare, and mysql provides a lot of date functions.