Know How SQL Server Executes a Query in Short

The present blog explains the entire process as to how MS SQL server, the client-server platform executes a query request, issued by user. Sendingrequests, which is the only means of communication with the server database can be achieved by the use of some specific in-built commands that the database in SQL server understands. The reason for acquiring the way SQL server executes a query is that it shall assist developers to write better coding for the database. Though understanding requires a complete know how and is a lengthy task, an attempt has been made to make users acquaint with the process in short by means of the image displayed below.



Summary of ‘Query Execution’ Procedure The complete procedure ofquery execution can be summarized as below:
                                                                                          
Step 1: At the very beginning a Request is sent, the Request in turn creates a new Task. In fact this step involves Creation of Task.

Step 2: If workers are not free to take up a Task for execution, a queue of Task gets formed and remains in pending state for execution.

Step 3: When any Worker in the threadpool becomes free after the execution of any Task, the idle Worker then picks up a pending Task. This step is all about an idle worker picking up a Pending Task.

Step 4: This is the Task Execution step of the query execution process. Here, firstly an execution plan is compiled. This in turn involves parsing, compilation and optimization. Thereafter, Query plan is executed where the operators access data through the buffer pool.

Step 5: Then, Result set is returned during execution.

Step 6: Finally, at the end the Task gets executed and complete. The involved Worker returns to idle state and the process gets over.

The process of SQL Server execution of queries can be understood easily by the below mentioned steps.

1.    Creation of ‘Request’

At the very beginning when a new request is completely sent over the Tabular Data Stream (TDS), a protocol that is used for communication between the sender’s application and the server database; task is created by server database engine for handling the placed request. Once the request gets formed it can take any one of the three below mentioned forms.

1.    Batch Request
2.    Remote Procedure Call Request
3.    Bulk Road Request

Note - The list of requests in the server can be queried fromsys.dm_exec_requests.

2.    Creation of ‘Task’

The task that is created to handle the request represents the request right from the start to end. When a new request reaches the server and then the task that is created to handle the particular request remains in ‘pending’ state. The reason being, that at the current stage the server has no clue as to what exactly the request signifies. Thus, a pile of tasks gets formed and remains in queue. In such a case the task has to be executed first, for which the server database engine should assign a worker to it.

Note - The list of tasks in the server can be attained by sys.dm_os_tasksquery. 

3.    Assigning ‘Worker’ a Task

Workers are one of the main components and actually the threadpool of SQL Server. Multiple of them are created in the beginning i.e. at the very start of the server. Although more of them can be formed on-demand up to a limit of configured maximum worker threads. All the workers remain in waiting state for the ‘pending’ tasks to become available, from requests coming into the server. Thereafter, each idle worker picks up exactly one task and executes it. In the mean time all the engaged worker remains unavailable until the execution of task gets over. Therefore, Tasks that remain in ‘pending’ state due to lack ofavailable workers, shall have to stay pending until execution of any task gets complete and the worker that executed the particular task becomes available.

Note - The lists and state of workers inside SQL Server can be checked by querying sys.dm_os_workers.

4.    Parsing and Compilation

Thereafter, an execution plan is complied. In this step, before a Task starts executing a Request the first thing it needs to do is to gain a proper understanding about the content of the specific Request. The T-SQL text that exists inside the request gets parsed and an abstract syntax tree gets formed for representation of the particular Request. Actually, the conclusion is that all the existing Requests are parsed and thereby compiled in this step. However, in case any error occurs at this stride the Requests gets terminated with a compilation error.

5.    Optimization

Optimization is the next important phase in the complete life cycle of Task Execution, so as to select an optimum data path access. In case of SQL server, the best way of optimization is chosen by first observing the costs of each possible alternative. Thereby, the alternative with the lowest cost is chosen as the query plan to be utilized, to make the process economical. It is quite obvious that exploring all the possible alternatives consumes much time. Hence, once a query plan is created it is also cached, for use in future. So, similar Requests that might be requested in future can skip over the optimization phase; provided an already compiled and optimized query plan is found in the internal cache of SQL Server. 

6.    Execution & Result

Finally, after compilation the Requests gets executed, the Task gets completed and the Worker becomes available and free to pick up another Task in pending condition. Once a query plan is selected by the Optimizer, the ‘Request’ or say the ‘Query plan’ can be executed. Actually, this is the last important step as to how SQL server executes a query. Finally, the Result set is given back which is the end of the process.

Rules of IT Administration

  1. Remember the "K.I.S.S." principle..."keep isimple, Supervisor".
     
  2. If you have to assign permissions to one entity you'll have to assign them to another.

    Similar, if I'm assigned to complete a particular task once I'll likely be assigned the task again.
     
  3. The workers change but the work stays pretty much the same.
     
  4. Assign permissions to a Group | Role rather than to an individual user.
     
  5. It's easier to manage membership than it is to manage permissions.
     
  6. Beware: anything that I can "engineer", I can also "over-engineer".
     
  7. Remember: just because I can, doesn't mean I should.
     
  8. When in doubt in Windows, right click.
     
  9. I can't turn to the left or right in Windows | Exchange | SQL Server without being permitted to do so.
     
  10. If I'm in I.T. and there's a problem with a computer it's my fault.
     
  11. The add on rule to Rule #8: Even if it's not my fault it's my fault.
     
  12. When you're designing an I.T. structure make sure that your design works hard for you rather than making you work hard.
I've copied this post from one of the site and it is wonderful to share with you all.

Changing SSH Port in Linux

Open ssh config with your favorite editor:
vi /etc/ssh/sshd_config
Look for line with “Port 22”, should be on top:
_ # What ports, IPs and protocols we listen for Port 12345_
Restart ssh service:
/etc/init.d/ssh restart

******************SAUDI APPS DBA***************************************

Migrate MySQL Databases, Users, and Privileges to Different Server

1.) Databases:
  • Get list of databases
1
mysql -uroot -proot_password -e "show databases"
  • Dump all databases
1
mysqldump -uroot -proot_password --all-databases > databases.sql
Note: you can dump only the databases that you want to move. Also, you need to exclude the “mysql” database itself if you are moving into an mysql server with existing records.
* Transfer sql dumps to new server with your preferred way (ftp, scp, rsync, etc)
  • Create Databases on new server.
1
mysql -uroot -proot_password -e "create database database_name_1;"
1
mysql -uroot -proot_password -e "create database database_name_2;"
1
mysql -uroot -proot_password -e "create database database_name_3;"
  • Import database dumps to new server
1
mysql -uroot -proot_password < databases.sql
2.) Database Users and Privileges:
  • Get list of database users and their privileges.
1
mysql -B -N -uroot -proot_password -e "SELECT user, host FROM user" mysql
  • Then get the privileges and password hash for each users from the above step.
1
mysql -B -N -uroot -proot_password -e "SHOW GRANTS FOR 'db_user'@'db_host'"
Note: Above command will output similar to these two lines or you will get more if the db_user has access to more than one databases.
1
GRANT USAGE ON *.* TO 'db_user'@'db_host' IDENTIFIED BY PASSWORD '*23AW9301879BCC8BC24343CDCDC40F375606C43D'
1
GRANT ALL PRIVILEGES ON db_name.* TO 'db_user'@'db_host'
  • Then you need to execute all these lines or queries on the new MySQL server. You can put them all in a text file (example: users.txt) and add semi-colon on each line, then import to mysql (example below):
1
mysql -uroot -proot_password < users.txt

HowTo Change Hostname in Linux

To check for current hostname:

1
hostname
Change hostname
1
hostname new_hostname
Make change permanent on reboot:
  • Debian/Ubuntu: edit or make changes on this file /etc/hostname
  • RHEL/CentOS: edit or make changes on this file /etc/sysconfig/network
Important:
Make sure to check or update new hostname in /etc/hosts as some running application could be using hostname values in there. Test running applications as well or try to reload/restart if possible (ex: apache).

Find out CPU, Memory, and Hard-disk information in Linux

Check for CPU/processor info and count:
>
cat /proc/cpuinfo | grep processor
Notes: Counting starts at 0. If the last processor id is 3 obviously you have 4 cpu cores. You can grep for “physical id” (ex: cat /proc/cpuinfo | grep “physical id”) to find out if cpus are virtual or not. If no physical id I think those are virtual cpu cores.
Check for total memory and usage. This also includes detail of swap memory:
>
free -m
Notes: ”-m” gives you the result in MB.
Check for hard drive space and usage:
>
df -h
Notes: ”-h” means human readable for size such as MB or GB, etc.
******************************SAUDI APPS DBA****************************************************