Welcome to Vinayak SQL Tutorial. In this Tutorials explaind completed SQL DBA & development attribute SQL Tutorial for Data Analysis

Query processing architecture guide

 

Query processing architecture guide


The SQL Server Database Engine processes queries on various data storage architectures such as local tables, partitioned tables, and tables distributed across multiple servers. The following sections cover how SQL Server processes queries and optimizes query reuse through execution plan caching.

Execution modes

The SQL Server Database Engine can process Transact-SQL statements using two distinct processing modes:

  • Row mode execution
  • Batch mode execution

Row mode execution

Row mode execution is a query processing method used with traditional RDBMS tables, where data is stored in row format. When a query is executed and accesses data in row store tables, the execution tree operators and child operators read each required row, across all the columns specified in the table schema. From each row that is read, SQL Server then retrieves the columns that are required for the result set, as referenced by a SELECT statement, JOIN predicate, or filter predicate.

Batch mode execution

Batch mode execution is a query processing method used to process multiple rows together (hence the term batch). Each column within a batch is stored as a vector in a separate area of memory, so batch mode processing is vector-based. Batch mode processing also uses algorithms that are optimized for the multi-core CPUs and increased memory throughput that are found on modern hardware.

When it was first introduced, batch mode execution was closely integrated with, and optimized around, the columnstore storage format. However, starting with SQL Server 2019 (15.x) and in Azure SQL Database, batch mode execution no longer requires columnstore indexes. For more information, see Batch mode on rowstore.

Batch mode processing operates on compressed data when possible, and eliminates the exchange operator used by row mode execution. The result is better parallelism and faster performance.

When a query is executed in batch mode, and accesses data in columnstore indexes, the execution tree operators and child operators read multiple rows together in column segments. SQL Server reads only the columns required for the result, as referenced by a SELECT statement, JOIN predicate, or filter predicate. For more information on columnstore indexes, see Columnstore Index Architecture.

SQL statement processing

Processing a single Transact-SQL statement is the most basic way that SQL Server executes Transact-SQL statements. The steps used to process a single SELECT statement that references only local base tables (no views or remote tables) illustrates the basic process.

Logical operator precedence

When more than one logical operator is used in a statement, NOT is evaluated first, then AND, and finally OR. Arithmetic, and bitwise, operators are handled before logical operators. For more information, see Operator Precedence.

In the following example, the color condition pertains to product model 21, and not to product model 20, because AND has precedence over OR


SQL
SELECT ProductID, ProductModelID
FROM Production.Product
WHERE ProductModelID = 20 OR ProductModelID = 21
  AND Color = 'Red';
GO

You can change the meaning of the query by adding parentheses to force evaluation of the OR first. The following query finds only products under models 20 and 21 that are red.

SQL

SELECT ProductID, ProductModelID
FROM Production.Product
WHERE (ProductModelID = 20 OR ProductModelID = 21)
  AND Color = 'Red';
GO

Optimize SELECT statements

A SELECT statement is non-procedural; it doesn't state the exact steps that the database server should use to retrieve the requested data. This means that the database server must analyze the statement to determine the most efficient way to extract the requested data. This is referred to as optimizing the SELECT statement. The component that does this, is called the Query Optimizer. The input to the Query Optimizer consists of the query, the database schema (table and index definitions), and the database statistics. The output of the Query Optimizer is a query execution plan, sometimes referred to as a query plan, or execution plan. The contents of an execution plan are described in more detail later in this article.



Android SQLite :-Android SQLite Database Example Tutorial

 Android SQLite

Android SQLite is a very lightweight database which comes with Android OS. Android SQLite combines a clean SQL interface with a very small memory footprint and decent speed. For Android, SQLite is “baked into” the Android runtime, so every Android application can create its own SQLite databases. Android SQLite native API is not JDBC, as JDBC might be too much overhead for a memory-limited smartphone. Once a database is created successfully its located in data/data//databases/ accessible from Android Device Monitor. SQLite is a typical relational database, containing tables (which consists of rows and columns), indexes etc. We can create our own tables to hold the data accordingly. This structure is referred to as a schema.

Android SQLite SQLiteOpenHelper

Android has features available to handle changing database schemas, which mostly depend on using the SQLiteOpenHelper class. SQLiteOpenHelper is designed to get rid of two very common problems.

  1. When the application runs the first time - At this point, we do not yet have a database. So we will have to create the tables, indexes, starter data, and so on.
  2. When the application is upgraded to a newer schema - Our database will still be on the old schema from the older edition of the app. We will have option to alter the database schema to match the needs of the rest of the app.

SQLiteOpenHelper wraps up these logic to create and upgrade a database as per our specifications. For that we’ll need to create a custom subclass of SQLiteOpenHelper implementing at least the following three methods.

  1. Constructor : This takes the Context (e.g., an Activity), the name of the database, an optional cursor factory (we’ll discuss this later), and an integer representing the version of the database schema you are using (typically starting from 1 and increment later).

  1. public DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
    
  2. onCreate(SQLiteDatabase db) : It’s called when there is no database and the app needs one. It passes us a SQLiteDatabase object, pointing to a newly-created database, that we can populate with tables and initial data.

  3. onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) : It’s called when the schema version we need does not match the schema version of the database, It passes us a SQLiteDatabase object and the old and new version numbers. Hence we can figure out the best way to convert the database from the old schema to the new one.

We define a DBManager class to perform all database CRUD(Create, Read, Update and Delete) operations.

Opening and Closing Android SQLite Database Connection

Before performing any database operations like insert, update, delete records in a table, first open the database connection by calling getWritableDatabase() method as shown below:

public DBManager open() throws SQLException {
        dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

The dbHelper is an instance of the subclass of SQLiteOpenHelper. To close a database connection the following method is invoked.

 public void close() {
        dbHelper.close();
    }

Inserting new Record into Android SQLite database table

The following code snippet shows how to insert a new record in the android SQLite database.

public void insert(String name, String desc) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(DatabaseHelper.SUBJECT, name);
        contentValue.put(DatabaseHelper.DESC, desc);
        database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
    }

Content Values creates an empty set of values using the given initial size. We’ll discuss the other instance values when we jump into the coding part.

Updating Record in Android SQLite database table

The following snippet shows how to update a single record.

public int update(long _id, String name, String desc) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.SUBJECT, name);
        contentValues.put(DatabaseHelper.DESC, desc);
        int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
        return i;
    }

Android SQLite - Deleting a Record

We just need to pass the id of the record to be deleted as shown below.

public void delete(long _id) {
        database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper._ID + "=" + _id, null);
    }

Android SQLite Cursor

A Cursor represents the entire result set of the query. Once the query is fetched a call to cursor.moveToFirst() is made. Calling moveToFirst() does two things:

  • It allows us to test whether the query returned an empty set (by testing the return value)
  • It moves the cursor to the first result (when the set is not empty)

The following code is used to fetch all records:

 public Cursor fetch() {
        String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.SUBJECT, DatabaseHelper.DESC };
        Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

Another way to use a Cursor is to wrap it in a CursorAdapter. Just as ArrayAdapter adapts arrays, CursorAdapter adapts Cursor objects, making their data available to an AdapterView like a ListView. Let’s jump to our project that uses SQLite to store some meaningful data

MS SQL Server - Execution Plans

  MS SQL Server - Execution Plans

Query optimizer with the help of statistics and Algebrizer\processor tree. It is the result of Query optimizer and tells how to do\perform your work\requirement.

There are two different execution plans - Estimated and Actual

Estimated execution plan indicates optimizer view.

Actual execution plan indicates what executed the query and how was it done.

Execution plans are stored in memory called plan cache, hence can be reused. Each plan is stored once unless optimizer decides parallelism for the execution of the query.

There are three different formats of execution plans available in SQL Server - Graphical plans, Text plans, and XML plans.

SHOWPLAN is the permission which is required for the user who wants to see the execution plan.

Example 1

Following is the procedure to view the estimated execution plan.

Step 1 − Connect to SQL Server instance. In this case, 'TEST is the instance name as shown in the following snapshot.



Step 2 − Click on New Query option on the above screen and write the following query. Before writing the query, select the database name. In this case, 'TestDB' is database name.

Select * from StudentTable


Step 3 − Click the symbol which is highlighted in red color box on the above screen to display the estimated execution plan as shown in the following screenshot.







Step 4 − Place the mouse on table scan which is the second symbol above the red color box in the above screen to display the estimated execution plan in detail. The following screenshot appears.










SQL Server Reporting Services (SSRS)

 

What is SQL Server Reporting Services (SSRS)?

SQL Server Reporting Services (SSRS) provides a set of on-premises tools and services that create, deploy, and manage reports. You can design reports using data, tables, graphs, charts, and images. You can easily deploy reports on the local or remote server.

Step 1

Let us first download installation media for SSRS. To download compatible installation media first run SQL Server Setup as shown in this article. Go to the Install SQL Server 2019 Developer Edition section of the article and follow steps 1 and 2. You will see the below screen. Now click on the Install SQL Server Reporting Services link and it will launch a download page for SSRS. Download installation media for SSRS report from that page


Step 2

Now double click and run the downloaded installation media. You will see the below screen. Click on the Install Reporting Services button.



Step 3

It will start the installation of SSRS which will take some time.



Step 4

Next, you will see the configuration wizard. First, you need to connect an SQL server instance for which you want to configure SSRS. Select an instance and click on the Connect button.


Step 5

Next click on the Service Account tab from the left panel. You will see the service account configuration window. Specify windows account to run the report server service. I recommend creating a new dedicated user with administrator privileges for this. I have created one with the name ReportAdmin. Enter your account and password and click on Apply button. It will configure the service account.



Step 6

Next click on the Web Service URL tab from the left panel. Here you can configure the report server URL. We will use this URL to deploy SSRS reports on the report server. You can also configure ports (80 or 443 (SSL)) on which the report server will host. It will preview URLs in the bottom section. I will go with default settings. Click on the Apply button and it will configure the report server web service URL.


Step 7

Next click on the Database tab from the left panel. Here we will configure the database for the report server. Click on the Change Database button.








.









SQL Injection

 SQL injection is a code injection technique that might destroy your database.

SQL injection is one of the most common web hacking techniques.


explain how to find and exploit various kinds of SQL injection vulnerabilities, and summarize how to prevent SQL injection.


SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries that an application makes to its database. It generally allows an attacker to view data that they are not normally able to retrieve. This might include data belonging to other users, orany other data that the application itself is able to access. In many cases, an attacker can modify or delete this data, causing persistent changes to the application's content or behavior.





SQL Server Architecture Explained: Named Pipes, Optimizer, Buffer Manager

SQL Server Architecture Explained: Named Pipes, Optimizer, Buffer Manager 

MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a request. The SQL Server accepts, processes and replies to the request with processed data. Let’s discuss in detail the entire architecture shown below:

As the below Diagram depicts there are three major components in SQL Server Architecture:

   1.  Protocol Layer

   2 .Relational Engine



      Protocol Layer – SNI

·        Shared Memory

·        TCP/IP

·        Named Pipes

·        What is TDS?



Shared Memory
MS SQL SERVER – Here MS SQL server provides SHARED MEMORY PROTOCOL. Here CLIENT and MS SQL server run on the same machine. Both can communicate via Shared Memory protocol

For connection to local - DB in SQL Management Studio, "Server Name" Option could be below

 

TCP/IP
MS SQL SERVER provides the capability to interact via TCP/IP protocol, where CLIENT and MS SQL Server are remote to each other and installed on a separate machine.

Named Pipes
MS SQL Server Provides the capability to interact via the named pipe protocol, here the CLIENT and MS SQL Server are in connection via LAN


What is TDS?
Now that we know that there are three types of Client-Server Architecture, lets us have a glance at TDS:


TDS stands for Tabular Data Stream.

All 3 protocol Relational Engine
The Relational Engine is also known as the Query Processor. It has the SQL Server components that determine what exactly a query needs to do and how it can be done best. It is responsible for the execution of user queries by requesting data from the storage engine and processing the results that are returned.

As depicted in the Architectural Diagram there are 3 major components of the Relational Engine. Let’s study the components in detail:



CMD Parser
Data once received from Protocol Layer is then passed to Relational Engine. “CMD Parser” is the first component of Relational Engine to receive the Query data. The principal job of CMD Parser is to check the query for Syntactic and Semantic error. Finally, it generates a Query Tree. 



Syntactic check:

Like every other Programming language, MS SQL also has the predefined set of Keywords. Also, SQL Server has its own grammar which SQL server understands.
SELECT, INSERT, UPDATE, and many others belong to MS SQL predefined Keyword lists.
CMD Parser does syntactic check. If users’ input does not follow these language syntax or grammar rules, it returns an error.

Semantic check:

This is performed by Normalizer.
In its simplest form, it checks whether Column name, Table name being queried exist in Schema. And if it exists, bind it to Query. This is also known as Binding.
Complexity increases when user queries contain VIEW. Normalizer performs the replacement with the internally stored view definition and much more.

Create Query Tree:

This step generates different execution tree in which query can be run.
Note that, all the different trees have the same desired output.

Optimizer
The work of the optimizer is to create an execution plan for the user’s query. This is the plan that will determine how the user query will be executed.


Similarly, MS SQL Optimizer works on inbuilt exhaustive/heuristic algorithms. The goal is to minimize query run time. All the Optimizer algorithms are propriety of Microsoft and a secret. Although, below are the high-level steps performed by MS SQL Optimizer. Searches of Optimization follows three phases as shown in the below diagram:




Phase 0: Search for Trivial Plan:
This is also known as Pre-optimization stage.
For some cases, there could be only one practical, workable plan, known as a trivial plan. There is no need for creating an optimized plan. The reason is, searching more would result in finding the same run time execution plan. That too with the extra cost of Searching for optimized Plan which was not required at all.If no Trivial plan found, then 1st Phase starts

Phase 1: Search for Transaction processing plans
This includes the search for Simple and Complex Plan.
Simple Plan Search: Past Data of column and Index involved in Query, will be used for Statistical Analysis. This usually consists but not restricted to one Index Per table. Still, if the simple plan is not found, then more complex Plan is searched. It involves Multiple Index per table..

Phase 2: Parallel Processing and Optimization.
If none of the above strategies work, Optimizer searches for Parallel Processing possibilities. This depends on the Machine’s processing capabilities and configuration If that is still not possible, then the final optimization phase starts. Now, the final optimization aim is finding all other possible options for executing the query in the best way. Final optimization phase Algorithms are Microsoft Propriety.


Storage Engine
The work of the storage Engine is to store data in storage system like disk or san and retrieve the data when needed before we deep dive into storage engine , lets have a look at how data is stored database and type of tiles available

Data File and Extent


Data File, physically stores data in the form of data pages, with each data page having a size of 8KB, forming the smallest storage unit in SQL Server. These data pages are logically grouped to form extents. No object is assigned a page in SQL Server.

The maintenance of the object is done via extents. The page has a section called the Page Header with a size of 96 bytes, carrying the metadata information about the page like the Page Type, Page Number, Size of Used Space, Size of Free Space, and Pointer to the next page and previous page, etc.




Primary file
Every database contains one Primary file.
This store all important data related to tables, views, Triggers, etc.
Extension is .mdf usually but can be of any extension.

Secondary file
Database may or may not contains multiple Secondary files.
This is optional and contain user-specific data.
Extension is .ndf usually but can be of any extension.

Log file
Also known as Write ahead logs.
Extension is .ldf
Used for Transaction Management.
This is used to recover from any unwanted instances. Perform important task of Rollback to uncommitted transactions.

Access Method

It acts as an interface between query executor and Buffer Manager/Transaction Logs.

Access Method itself does not do any execution.

The first action is to determine whether the query is:

Select Statement (DDL)

Non- Select Statement (DDL & DML)

Depending upon the result, the Access Method takes the following steps:

If the query is DDL, SELECT statement, the query is pass to the Buffer Manager for further processing.
And if query if DDL, NON-SELECT statement, the query is pass to Transaction Manager. This mostly includes the UPDATE statement


Buffer Manager
Buffer manager manages core functions for modules below:

Plan Cache
Data Parsing: Buffer cache & Data storage
Dirty Page

We will learn Plan, Buffer and Data cache in this section. We will cover Dirty pages in the Transaction


Plan Cache
Existing Query plan: The buffer manager checks if the execution plan is there in the stored Plan Cache. If Yes, then query plan cache and its associated data cache is used.
First time Cache plan: Where does existing Plan cache come from?
If the first-time query execution plan is being run and is complex, it makes sense to store it in in the Plane cache. This will ensure faster availability when the next time SQL server gets the same query. So, it’s nothing else but the query itself which Plan execution is being stored if it is being run for the first time.



 














SQL Performance Improvement


SQL Performance Improvement

  1. SQL query optimization basics
  2. Add missing indexes
  3. Check for unused indexes
  4. Avoid using multiple OR in the FILTER predicate
  5. Avoid too many JOINs.

Missing Indexes

SQL Server, via the Management Studio GUI, execution plan XML, or missing index DMVs, will let us know when there are missing indexes that could potentially help a query perform better:



This warning is useful in that it lets us know that there is a potentially easy fix to improve query performance. It is also misleading in that an additional index may not be the best way to resolve a latency issue. The green text provides us with all of the details of a new index, but we need to do a bit of work before considering taking SQL Server’s advice:
Avoid Running Queries in a Loop

Running queries in a loop can significantly slow your runtime. In some cases, you may be able to bulk insert and update data, which is far more efficient than using loops.

How can you select which queries to optimize?

Before you can optimize any queries, you have to decide which ones are best to optimize. Unfortunately, many people skip this critical step, but by targeting specific, troublesome queries with significant impacts on execution time, you can dramatically increase performance.
If you are less selective when deciding which queries to optimize, you may end up wasting time and money by optimizing those that don’t significantly contribute to performance, don’t impact other queries, or don’t result in problems users will notice.
When starting the MS SQL database query optimization process, look for queries that are consistently or occasionally slow, have red flags, or are major contributors to the total execution time.

Queries That Majorly Contribute to Total Execution Time

If more than five percent of total execution time can be attributed to a single query, try to optimize the query in question. Focusing your efforts on optimizing major contributors rather than those that don’t significantly contribute to performance will enable you to use your time more efficiently.