SQL in Depth (in 4D v11)
[These are notes from a session at 4D Summit 2008]
Presenter: Sergiy Temnikov, 4D SAS
It's important to realize that the SQL engine is not "on top of" DB4D, it's fused into it. There's definitely no penalty for using SQL.
v11.3 is a lot more than a dot release - there are a bunch of huge improvements to the engine.
- String searches 1.6x faster, numeric is 2 times faster,
- Getting the data out of the db into your arrays is 1.3 times faster,
- Numeric sorting is 1.2 times faster,
- Expression searches are 1.1 times faster,
- Numeric grouping (GROUP BY) is 1.2-1.3 times faster,
- String grouping is exponentially faster (!) depending on the number of records
- Min, Max, Avg, Sum, Count is 1.2 to 2 times faster
Connectivity options...
Previously...
- ODBC LOGIN (DSN) - traditional, requires configuration
- ODBC LOGIN (SQL_INTERNAL) is using things that look like ODBC, but it's not actually using ODBC.
- USE EXTERNAL DATABASE
v11.3 has simplied it - now it's just SQL LOGIN / SQL LOGOUT. They're renaming the commands so there's just the one entry point. Takes the same parameters as ODBC LOGIN. USE EXTERNAL DATABASE is still there, but deprecated - there's now a 4th parameter to SQL LOGIN that should be used instead. (BTW, DSNs can be preceded with ODBC:.)
v11.3 also allows direct/native SQL connections from 4D (anything) to 4D Server. You pass 4D:My_4D_Server (which is why ODBC: is needed for ODBC). Or you can pass something like IP:122.168.12.15 and just pass the IP address. Going native means no ODBC overhead. It's the fastest way to go 4D to 4D - 2 times faster than ODBC to login, and 3-10 times faster for data fetching, bulk inserts (with arrays) are 2-5 times faster. On top of that the error handling is far better (the entire error stack is streamed to the client).
And on top of that the 4D to 4D connection is secure, even without SSL. Every message is signed so they can't inject anything by pretending to be you, and usernames and passwords are not sent in clear text. HOWEVER, they can see the data. If you want to avoid that, use SSL (preference check box).
GET LAST SQL ERROR has been changed to GET LAST ERROR STACK and gives very detailed errors when doing 4D to 4D SQL connections.
SQL access rights have been beefed up in v11.3 as well. In v11.2 you have three groups of users with global rights throughout the database - read only group, read write group, and full access group. v11.3 allows you to have different access rights for different tables. It uses SQL schemas to do this. Every table must belong to one and only one schema, so schemas are groupings of one or more tables. They have implemented DEFAULT_SCHEMA and assigned all tables to that schema that makes the transition from 11.2 to 11.3 completely easy. If you want the schema can have just one table so you can have table-based access rights if you want. There is a new table in the system catalog that describes all the schemas.
There is a component available that will show all the schema in your database visually, but you have to set up schemas using SQL commands. The component will write the SQL statements for you. Once a schema is set up you can see/choose the schema in the table properties dialog. When you grant rights you grant them to a group, not an individual user and its done through SQL code.
People without rights can see that tables exist, but can't get the data in them.
On SQL Authenticate has changed. Previously you just set $0 to true. Now you need to call CHANGE CURRENT USER and the call has to be successful.
There is also a new multi-row insert capability.
There is also a new INFILE command to pull data from a file which is faster than loading the file into a variable. You can import text, pictures and blobs using INFILE.
You can also put BLOB data into picture arrays. This way you don't have to handle blob data differently than other data when pulling data into arrays. Just use PICTURE TO BLOB ($pict;$blob;".4DBlob") to get the blob out of the picture. However, if you have very large blobs it's still better to get them one by one since you may not need the blob data.

[These are notes from 4D Summit 2008]
[Notes from 4D Summit 2008.]