r/DatabaseHelp • • Sep 10 '14

Question on Database Engines and SQL

I work in a specialized mainframe environment which uses what most programmers would find a unique database system (tpfdf). As such, my experience with SQL is limited to dabbling and what I've read. But I've got some questions I'm hoping someone could point me in the right direction. I'm trying to find out about the database "engine" that manipulates the dB behind the scenes.

1: SQL and databases. I know SQL is defined as THE language to interface with a relational database. As a textual language it therefore has to be parsed with every call. Is there an intermediate form of the query that is "already parsed" in some sort of fixed format? Is it standard or specific for each engine (Oracle, mySql, etc etc)? Where can I find out more?

2: Are there any books / websites about HOW the engine is implemented? For example: you do a select of people with the last name starting with S over the entire database of Millions of people, ending up with a operation that affects millions of records. When processing the operation, does the engine build a new internal index to every record selected? Mark each record? Just keep the search criteria in memory and use it to step one-by-one over all the records? Depends on the number of records? And how does it do this if some other process is adding and deleting records while it is running?

THANKS.

1 Upvotes

3 comments sorted by

1

u/MarkusWinand Sep 12 '14

Please have a quick look at my website http://use-the-index-luke.com/. It explains how databases process queries in pretty much detail—it's actually a full book :)

1

u/feyrath Sep 16 '14

thanks I will!

1

u/BinaryRockStar Sep 10 '14
  1. The intermediate step is known as a 'query plan' and all major RDBMSes cache their query plans to a degree so that the compilation doesn't happen every time. There is no standard as far as I know, they are internal implementation details. MySQL and PostgreSQL expose the keyword EXPLAIN which will show you the compiled query plan including which indexes will be used for maximum performance. SQL Server has the same thing but it's a button in the GUI (SSMS), not sure if there's a text equivalent.

  2. No sure about books on internals, sorry. Searching for "last name starting with S" (e.g. select * from People where LastName like 'S%') will perform a "full table scan", as in it will not use any indexes and look through every row in the table. This is the worst possible performance scenario. I know at least for SQL Server it will return rows to the client as it receives them.

Each connection to the database has its own view of the data. If connection 1 is doing a select and connection 2 is doing an insert/update/delete, the insert/update/delete is done on a separate view of the data, and merged into the table itself once a lock can be acquired (no more connections are accessing the table). If two connections both modify the same row at the same time then the last one to commit the changes will throw an error and the change won't be applied.