What Does an SQL Query Contain?

Database Dive
Database Dive
44 بار بازدید - 3 ماه پیش - What does an SQL query
What does an SQL query contain?

This SQL tutorial will walk you through the possible answers to this database question.

The answer to this SQL question is hidden in the fact that SQL queries are tasks that are composed of other tasks. To make the performance of your SQL queries faster in your database management system, we have to make the performance of those tasks faster, or get rid of them altogether.

To know what SQL queries contain, we need to profile them. Depending on the database management system you find yourself using, profiling can be as simple as running a SQL query like SET profiling = 1;, then executing the necessary SQL queries (the database queries you want to execute), and then glancing at the query ID by running SHOW PROFILE FOR QUERY [id] after identifying the query ID with the SHOW PROFILES; SQL query. In this example MariaDB tutorial, we use MariaDB Server. Other database management systems may use different approaches.

After profiling your SQL query in your database management system, you will see a couple of status codes.
These status codes may differ depending on the DBMS that is in use and this MariaDB tutorial will tell you that in MariaDB they are as follows:

1) Starting – The query is starting.
2) Checking permissions – Your database is checking whether your user has the necessary permissions.
3) Opening tables – Your database is opening all tables for operations.
4) After opening tables (MariaDB specific) - Your database is conducting operations after opening a table.
5) System lock – Your database is waiting for a system lock on the table to be released if it's in place.
6) table lock – Your database is waiting for a table lock on the table to be released if it's in place.
7) init – Your database is initializing the query.
8) Optimizing – Your database performs internal processes to determine how best to execcute your query.
9) Statistics – Your database is calculating statistics-related data for the query execution plan.
10) Preparing – Your database is preparing to run the query.
11) Executing – Your database is executing the query.
12) Sending data – Your database is sending the data returned by the query back to you.
13) End of update loop – Your database finishes running the query and is ready to clean up.
14) Query end – Your database has finished executing the query.
15) Commit – Your database is saving changes (committing.)
16) closing tables – The tables that were opened and impacted by the query are being closed.
17) Unlocking tables – Your database is unlocking tables that were previously locked.
18) Starting cleanup – Your database is preparing to clean up the items in the memory.
19) Freeing items – Your database is freeing items that were used to execute a query.
20) Updating status – Your database is updating its status and getting ready to run upcoming SQL queries.
21) Reset for next command – Your database is cleaning up the items in the memory and resetting internal variables to prepare to run an upcoming query.

To make the performance of your SQL queries faster, make these tasks execute faster or eliminate them altogether.
For INSERT SQL queries, consider bulk inserting methods, locking tables, inserting data into them, and unlocking them afterwards. Also consider ditching INSERT database queries in favor of LOAD DATA INFILE SQL queries altogether. LOAD DATA INFILE SQL queries will be significantly faster than INSERT SQL queries because they avoid the overhead associated with INSERT queries by inserting raw data.
For SELECT SQL queries, consider selecting only the necessary rows swapping SELECT * for SELECT column if necessary, adding database indexes or partitioning your database on the necessary columns if you have a lot of data, normalize your tables, and avoid searching for entries with leading wildcards in front of SELECT SQL queries.
For UPDATE queries, consider locking tables (see the SQL query #1 below), performing many updates one by one, and then unlocking them by making use of the LIMIT clause, and for DELETE SQL queries, make use of WHERE and LIMIT possibilities, and finally, if necessary, swap DELETE for TRUNCATE too.

That's the crux of it - there are many other things that help you optimize your database query performance, but these go beyond the contents of SQL queries.
To learn more about database performance, availability, security, and other metrics, subscribe to this database tutorial channel, and until next time.

SQL query #1:
---
LOCK TABLE [WRITE|READ] x;

UPDATE x SET column = ‘value’ LIMIT 0,50000;
UPDATE x SET column = ‘value’ LIMIT 50000,100000;
UPDATE x SET column = ‘value’ LIMIT 100000,150000;
...
UNLOCK TABLE x;
---

Music:
Blame It on the Summer by Lizlism: SoundCloud: lizlism
Creative Commons — Attribution 3.0 Unported
Free Download / Stream: https://bit.ly/3Vm82Fk
Music promoted by Audio Library https://bit.ly/4aN4C40

#sql #coding #database #software #programming #data
3 ماه پیش در تاریخ 1403/03/25 منتشر شده است.
44 بـار بازدید شده
... بیشتر