News for hackers. Stuff that matters.
Re:Many small queries are efficient in SQLite by hans_castorp on Tuesday February 16, 2021 @02:05AM (#26151622)
I will re-consider it, once they have proper data types and data type checking.
Re:Many small queries are efficient in SQLite by jraph on Tuesday February 16, 2021 @02:50AM (#26151816)
This aspect will probably never change:

> Flexible typing is considered a feature of SQLite, not a bug. Nevertheless, we recognize that this feature does sometimes cause confusion and pain for developers who are acustomed to working with other databases that are more judgmental with regard to data types. In retrospect, perhaps it would have been better if SQLite had merely implemented an ANY datatype so that developers could explicitly state when they wanted to use flexible typing, rather than making flexible typing the default. But that is not something that can be changed now without breaking the millions of applications and trillions of database files that already use SQLite's flexible typing feature.

https://sqlite.org/quirks.html#flexible_typing

Re:Many small queries are efficient in SQLite by rini17 on Tuesday February 16, 2021 @03:09AM (#26151902)
It is possible to create check constraints that do the type checking. Actually, if they shipped some predefined ones and said "if you want strong type checking do this", like, syntax to automatically populate the table columns with type check constraints, it would be IMO perfectly backward compatible.
Re:Many small queries are efficient in SQLite by darksaints on Tuesday February 16, 2021 @06:57AM (#26153102)
At the very least we could get strict versions of data types, or some sort of key word used in the ddl to specify strict typing.
Re:Many small queries are efficient in SQLite by attilakun on Tuesday February 16, 2021 @10:34AM (#26154807)
Re:Many small queries are efficient in SQLite by RedShift1 on Tuesday February 16, 2021 @03:15AM (#26151935)
When I started a project with SQLite, the available data types struck me as odd (coming from MySQL), but as I learned I started asking the question: are there any more fundamental data types other than null, int, real, text and blob? For example dates are just a facade for an integer of some kind, JSON is really just text adhering to certain formatting rules, booleans are usually stored as some kind of byte anyway so why not drop that abstraction?

With this limited set of datatypes it really makes you think harder about the data you are processing, because in the end all your data is one of these types anyway.

Re:Many small queries are efficient in SQLite by hans_castorp on Tuesday February 16, 2021 @03:42AM (#26152091)
> are there any more fundamental data types other than null, int, real, text and blob?

Those type names are just hints, they don't constrain the valid values in any way.

https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=4634e3821676ed...

Re:Many small queries are efficient in SQLite by yread on Tuesday February 16, 2021 @06:06AM (#26152846)
I don't really see it as such a problem. When interacting with the database you either use an ORM with your strongly-typed business objects or you write the SQL and use parameters (again strongly typed)
Re:Many small queries are efficient in SQLite by hans_castorp on Tuesday February 16, 2021 @11:00AM (#26155108)
> When interacting with the database you either use an ORM with your strongly-typed business objects

If you have strongly-type business objects then why not have a strongly typed storage? If you code can control the constraints that a correct data type ensures, then why have "strongly type business objects" to begin with? Why not store everything in your code as strings as well?

I see this misconception all the time. The database (and its data) lives way longer than most applications. And it's also a wrong to assume that there is always only one application accessing the database. Bulk loads are a typical case of secondary applications.

Not choosing the proper data type in a relational database is a really bad decision and we see question on stackoverflow and similar sites on a weekly (if not daily) basis asking how to fix invalid data in those "un-typed" columns.

Re:Many small queries are efficient in SQLite by yread on Tuesday February 16, 2021 @04:08PM (#26158833)
I'm not saying it's useless to have type constraints in a database. Just that with a bit of care it's not such a big deal.

In the end there will always be some business rules that are not constrained by the database. So, you always have to be a bit careful about what you store in it. Indeed not being careful and hoping that your types, constraints and triggers are going to save you is more risky

> Not choosing the proper data type in a relational database is a really bad decision

Well, then rejoice, you can't make this bad decision in SQLite because everything is +/- a number or a string

Re:Many small queries are efficient in SQLite by LunaSea on Tuesday February 16, 2021 @07:32AM (#26153274)
SQL parameters aren't strongly typed though and in any case SQLLite doesn't have stored procedures.
Re:Many small queries are efficient in SQLite by fctorial on Tuesday February 16, 2021 @05:00AM (#26152527)
Re:Many small queries are efficient in SQLite by RedShift1 on Tuesday February 16, 2021 @04:44AM (#26152438)
That is... interesting...
Re:Many small queries are efficient in SQLite by dietr1ch on Tuesday February 16, 2021 @04:27AM (#26152342)
I can't believe that runs. What a misfeature.
Re:Many small queries are efficient in SQLite by clon on Tuesday February 16, 2021 @04:36AM (#26152397)
You just put me off Sqlite for life. I can't believe someone thought this to be a good idea.
Re:Many small queries are efficient in SQLite by foepys on Tuesday February 16, 2021 @05:49AM (#26152759)
SQLite just does what JavaScript and other weakly typed languages do. The developer is responsible for what they input. If type checks are a must, SQLite is just not for you, easy as that.
Re:Many small queries are efficient in SQLite by LunaSea on Tuesday February 16, 2021 @07:33AM (#26153276)
But event JavaScript will reject data if it's considered not valid for a type.

It won't throw but it will set an "invalid" / "NaN" / etc. value.

Re:Many small queries are efficient in SQLite by foepys on Tuesday February 16, 2021 @08:04AM (#26153457)
I don't see how having "NaN" or "undefined" in a number field makes any difference to just putting the value there. If you are using a strongly typed language, this will not be a problem.

SQLite made a design choice in favor of simplicity. It's also missing basic date functions all together. The only way to compare dates is by using Unix Epoch.

Re:Many small queries are efficient in SQLite by samatman on Tuesday February 16, 2021 @05:19PM (#26159473)
I use ISO 8601 full timestamps in UTC.

They were carefully designed so that collation order is identical to temporal order. Which is convenient!

If you need interval logic, though, SQLite won't help you, and epoch is the better choice. It's possible to solve some queries with a regex, but you won't love it.

Re:Many small queries are efficient in SQLite by OskarS on Tuesday February 16, 2021 @10:01AM (#26154446)
Chill out. This is a design choice to make it more viable to be used in dynamically typed languages. It's maybe not the design choice everyone would make, but it's not, like, insane: this is how all variables in Python/JavaScript/Ruby/whatever works.

If you really care about this, adding strongly typed columns is trivial: https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=9baffa184672a7...

Re:Many small queries are efficient in SQLite by giantDinosaur on Tuesday February 16, 2021 @03:50AM (#26152145)
I mean, you can represent everything under the sun as a string (or a very big int) but that's a level of operating where I find my brain starts to get fatigued for absolutely no real gain at all.
Re:Many small queries are efficient in SQLite by magicalhippo on Tuesday February 16, 2021 @04:37AM (#26152405)
We need to store weights in kg with three decimal places. If we sum to get a total, the total should be correct according to those three digits.

I'm sure we could get it to work in SQLite, but it sounds like we would have to have some layer of manual fudging that we couldn't forget about.

In our current database we just use "numeric(16, 3)" and no worries.

Re:Many small queries are efficient in SQLite by em500 on Tuesday February 16, 2021 @04:52AM (#26152482)
At the risk of sounding glib, you could store everything as integer grams?
Re:Many small queries are efficient in SQLite by magicalhippo on Tuesday February 16, 2021 @05:01AM (#26152532)
Yes of course. That's why I said I'm sure we could get it to work. But then it has to be converted to kg for actual usage. Either directly in the queries or views.

Having a fixed-point decimal type allows us to not think about once the table is created.

Same issue with money. Most of the time we need to store monetary values with two decimal places (cents).

Re:Many small queries are efficient in SQLite by dmurray on Tuesday February 16, 2021 @05:38AM (#26152702)
What kind of application are you developing where there is so little middleware between the database storage layer and the user that converting to/from kilos adds significant additional complexity, but your users aren't technical enough to just deal with having weights in grams?
Re:Many small queries are efficient in SQLite by magicalhippo on Tuesday February 16, 2021 @06:12AM (#26152871)
These are electronic versions of official forms. The user interface and reports should show what is actually being sent or has been sent.

Sure we could deal with it, but there are quite a number of tables due to different forms and messages, and then there's all the reports. Many custom ones thanks to to local officials wanting data from a certain customer in a certain way...

Re:Many small queries are efficient in SQLite by scaladev on Tuesday February 16, 2021 @06:42AM (#26153019)
Bureaucratic forms have a tendency to change unpredictably. Today they want weight in kilograms, tomorrow they'll want it in grams, and the day after that in standardized intergalactic weight units. I get what you're saying (and I don't like writing unnecessary mapping boilerplate too), but there should be a middle layer to protect the application from craziness like that.
Re:Many small queries are efficient in SQLite by magicalhippo on Tuesday February 16, 2021 @06:58AM (#26153105)
Thankfully things in our sector is fairly static in that regard, so this is seldom an issue. That sounds quite horrible indeed.

And yes, a fatter middle layer would be nice. Our next generation software will probably have more of that, this code base is over 20 years old at this point...

Re:Many small queries are efficient in SQLite by magicalhippo on Tuesday February 16, 2021 @07:00AM (#26153113)
I realize this response turned out a lot more harsh than I intended. I was in a slight rush so didn't formulate myself well, didn't mean to be abrasive.
Re:Many small queries are efficient in SQLite by mvc on Tuesday February 16, 2021 @10:03AM (#26154460)
> Most of the time we need to store monetary values with two decimal places (cents).

Yeah who cares about fractions of pennies anyway. Just makes things complicated.

Re:Many small queries are efficient in SQLite by magicalhippo on Tuesday February 16, 2021 @10:25AM (#26154705)
Yeah for being so uptight about just about everything, the officials seems quite relaxed about values slipping between the cracks due to lack of decimal places.

Like, total invoice value can only be specified with two decimal digits, typically in foreign currency. Yet we also have to specify per-line value in local currency, also with only two digits. And then the per-line values are used to calculate taxes and whatnot...

Re:Many small queries are efficient in SQLite by jrochkind1 on Tuesday February 16, 2021 @12:45PM (#26156545)
The US IRS (tax agency) allows you to round nearly all values to dollars ignoring cents altogether.
Re:Many small queries are efficient in SQLite by RedShift1 on Tuesday February 16, 2021 @04:50AM (#26152474)
You could store it in grams as an integer.
Re:Many small queries are efficient in SQLite by darksaints on Tuesday February 16, 2021 @07:06AM (#26153140)
With this sort of perspective, why use a database at all? You can store literally any digital object as some sequence of bytes, and that's what file systems are for.

At some point, you actually like for the software you use to actually have meaningful features.

Re:Many small queries are efficient in SQLite by isbvhodnvemrwvn on Tuesday February 16, 2021 @08:38AM (#26153719)
Querying filesystems is extremely primitive when compared to what sqlite can do.
Re:Many small queries are efficient in SQLite by darksaints on Tuesday February 16, 2021 @10:05AM (#26154482)
So is storing dates as integers, and json as strings.
Re:Many small queries are efficient in SQLite by RedShift1 on Wednesday February 17, 2021 @05:21AM (#26164812)
But dates are integers (all databases store dates as an integer that is an offset from some fixed date), and JSONs are strings. SQLite has the functions to work with these datatypes (dates, JSON, etc...) but fundamentally they are not different from integers or strings.
Re:Many small queries are efficient in SQLite by darksaints on Wednesday February 17, 2021 @09:52AM (#26166881)
And integers are bytes and strings are bytes and tables are bytes and indexes are bytes. If you're not happy with databases being intelligent, why stop with integers and strings, when you can do everything you want with bytes?! Memcached, or LevelDB, or even a file system is the ultimate database if you follow your arguments to their logical conclusion.

But data representation isn't data. You can't look at a serialized sequence of bytes and know what it represents without context - without a serialization scheme. For relational databases, the table definition is the serialization scheme - it is the context. For example, you can't know what date an integer is representing without contextual information like the offset. By storing dates as dates in your database, that context is baked in.

It is helpful to have that full context inside your database because it allows you to operate that database more efficiently (by carefully indexing on the properties of the data, not the data representation), but it also allows you to use that database in ways that are not tightly coupled to your application, such as analytics, because you are storing data itself and not just the bare minimum required to represent that data.

Re:Many small queries are efficient in SQLite by aembleton on Tuesday February 16, 2021 @08:29AM (#26153634)
Because sqlite let's you index and query the data without having to write your own layer to do that.
Re:Many small queries are efficient in SQLite by darksaints on Tuesday February 16, 2021 @11:03AM (#26155139)
Having rich data types allows a lot of things without having to write your own layer. Spatial indexes are extremely efficient, and I can't query geometries efficiently when I'm storing geometries as a blob. The same goes for dates, json, XML, ranges, etc.

The problem with reserving specialized logic for the application layer is that it limits you to simplistic indexing schemes and you end up doing excessive IO and filtering in memory to get what you actually want.

The idea that databases shouldn't have specialized datatypes is really only an idea that works in simplistic crud apps. The world is much bigger than that.

Re:Many small queries are efficient in SQLite by shakkhar on Tuesday February 16, 2021 @04:35PM (#26159084)
FWIW, we use https://cgsql.dev/ to solve the type issue, among others. I totally understand that this is not for everyone.