European Windows 2019 Hosting BLOG

BLOG about Windows 2019 Hosting and SQL 2019 Hosting - Dedicated to European Windows Hosting Customer

SQL Server 2021 Hosting - HostForLIFE :: Remove CONVERTS/CASTS From WHERE/JOIN Clauses

clock March 30, 2022 08:28 by author Peter

Quick Tip
Remove CONVERT/CAST from your WHERE clauses and JOINS when comparing to variables of different data types. Set their data types to match your table definitions before using them as a filter. Optimizing your queries this way will greatly reduce the amount of CPU time, reads, and I/O generated in your queries and allow your code to take better advantage of indexes.

Example
We are going to create a very simple stored procedure called ConvertExample. In this procedure we will see two things. One, the first procedure we create will declare two variables as VARCHAR( MAX) data types, then in the WHERE clause it will convert a table column called Modified Date and compare the variables to that value. You’ll note using SET STATISTICS IO, TIME ON, this code takes considerably more CPU time compared to our second example that will CONVERT the variables first then compare it to a field without having to CONVERT in the WHERE clause.
USE [AdventureWorks2017]
GO
CREATE OR ALTER PROCEDURE [dbo].[ConvertExample]
(
       @fromDate AS VARCHAR(MAX),
       @toDate AS VARCHAR(MAX)
)
AS
BEGIN
SELECT [SalesOrderID]
      ,[SalesOrderDetailID]
      ,[CarrierTrackingNumber]
      ,[OrderQty]
      ,[ProductID]
      ,[SpecialOfferID]
      ,[UnitPrice]
      ,[UnitPriceDiscount]
      ,[LineTotal]
      ,[rowguid]
      ,[ModifiedDate]
  FROM [AdventureWorks2017].[Sales].[SalesOrderDetail]
  WHERE CONVERT(varchar(8), [ModifiedDate],112) BETWEEN @fromDate and @toDate
END
SET STATISTICS IO,TIME ON
GO


Now, turn on Actual Execution Plans and execute the procedure.
Execute dbo.[ConvertExample] '20110501','20110531'

Query Plan and Statistics IO, TIME results

 

This code generates a warning on our SELECT and generates 219ms CPU time. Also note the estimated number of rows 10,918. Now let’s rewrite the code by setting the variables to match the datatype of the field we want to filter on, Modified Date, which is a datetime.

--------------------------------------------------------------------------
--REWRITE Convert Variables Instead, and REMOVE CONVERT from WHERE Clause
-----------------------------------------------------------------------------
CREATE OR ALTER PROCEDURE [dbo].[ConvertExample]
(
       @fromDate AS VARCHAR(MAX),
       @toDate AS VARCHAR(MAX)
)
AS
BEGIN
SET @fromDate= CONVERT(dateTime, @fromDate)
SET @toDate= CONVERT(dateTime, @toDate)
SELECT [SalesOrderID]
      ,[SalesOrderDetailID]
      ,[CarrierTrackingNumber]
      ,[OrderQty]
      ,[ProductID]
      ,[SpecialOfferID]
      ,[UnitPrice]
      ,[UnitPriceDiscount]
      ,[LineTotal]
      ,[rowguid]
      ,[ModifiedDate]
  FROM [AdventureWorks2017].[Sales].[SalesOrderDetail]
  WHERE [ModifiedDate] BETWEEN @fromDate and @toDate
END
-----------------------------------------------------------------------------
--RERUN The Proc
-----------------------------------------------------------------------------
Execute dbo.[ConvertExample] '20110501','20110531'

Query Plan and Statistics IO, TIME results for the second version.

 

Note the large difference in CPU time, it drops from 219ms to 15ms. You’ll also note the type conversion warning is gone from our SELECT statement in the plan and the estimated number of rows is now 356 instead of 10918. Lastly, we also now have a missing index warning—with the conversion in place the query optimizer was unable to identity the missing index.

Now for fun let’s add an index on Modified date using the code below. Then rerun the old procedure and compare it to the new procedure. You may be surprised on what you see.

USE [AdventureWorks2017]
GO
CREATE NONCLUSTERED INDEX [IDX_SalesOrderDetail_Modifed Date] ON [Sales].[SalesOrderDetail]
(
       [ModifiedDate] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF)
GO

Now, Recompile the first version of the procedure, then Execute and look at the plan. Then Recompile the 2nd version and Execute and compare. Here is the original run. Note it uses a Clustered Index Scan and bypasses our index.

Now let’s look at the new one. You’ll see that by removing the CONVERT for every row it compares, it now can take advantage of the index we created. You’ll also note there is now a Key Lookup, take a look at this blog to find out how you can further optimize this query by getting rid of the Key Lookup.

This was just a quick tip and reminder that whenever possible you should remove CONVERTS\CASTS from your WHERE clauses and set the variables to proper data types instead. This same logic applies to JOINS. Do this not only to reduce the CPU time, I/O and reads, but to also take advantage of your indexes.

HostForLIFEASP.NET SQL Server 2021 Hosting

 



SQL Server Hosting - HostForLIFE :: Create SQL Server Database With Pre-Defined Schema

clock March 29, 2022 08:02 by author Peter

In many situations, we need to create a database at runtime. A few years ago I worked on a project which was a multi tenant system where we create a new database for each tenant. For this task we followed a few steps like creatoing a blank database, executing schema script (tables, functions, stored procedures, views, triggers, etc), then executing master data. In this process there are 2 major issues we faced, one is it takes time to execute all this from application, as connection should be live while doing these steps, and exception handling and rollback.

Basically, we need to understand how a database gets created. Whenever we create a new database in SQL Server it creates a copy of model database from system databases. It means that whatever there in model database will get replicated to the newly created database, like whole schema and data inside all tables.

To resolve this problem we have a solution which is very easy and less effort to fulfill this requirement. In SQL Server we have 4 default DBs inside System Databases. We need to use model DB for this. You can see this in below image,

We can add table schema, stored procedure, views, functions, and so on. I added one table and one stored procedure in model database as shown below,


Now when you create a new database whether it is from SSMS or from an application, it will replicate model db.

Now you can see model schema got replicated to newly created Database.


Here you can see one table with data, and one stored procedure got created. This is what I tried from SSMS, now we can check with C# code. please refer below,

And same DB got created as below,

This way we can use model database to generate pre-defined schema and data.

HostForLIFEASP.NET SQL Server 2019 Hosting



SQL Server Hosting - HostForLIFE :: DDL, DQL, DML, DCL, TCL with Examples

clock March 16, 2022 09:04 by author Peter

In this article, you will learn the SQL command categories and their sub-categories. SQL is an open-source data management system. The SQL query is used to retrieve and manipulate the data from the table. With the help of SQL command we can query, filter, sort, join, group and modify the data in the database.
SQL Commands

SQL commands are categorized into below 5 categories:

  • DDL – Data Definition Language
  • DQL – Data Query Language
  • DML – Data Manipulation Language
  • DCL – Data Control Language
  • TCL - Transaction Control Language

DDL (Data Definition Language)
DDL or Data definition language is actually the definition or description of the database structure or schema, it won't change the data inside the database. Create, modify, and delete the database structures, but not the data. Only These commands are not done by all the users, who have access to the database via an application.

CREATE
Create the database or its object (ie table, index, view, function etc.).

CREATE DATABASE databasename

Example
CREATE DATABASE Student_data;

Syntax
CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);

Example
CREATE TABLE Student (
   StudendId int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    Mark int
);


DROP
Drop command helps to delete the object from the database (ie table, index, view, function, etc.).
DROP object object_name

Example
DROP TABLE Student;

DROP DATABASE database_name

Example
DROP DATABASE Student_data;

ALTER
Alter command is helpful to change or modify the structure of the database or its object.
ALTER TABLE table_name
ADD column_name datatype


Example
ALTER TABLE Student
ADD Total int;


ALTER TABLE table_name
DROP COLUMN column_name


Example
​​​​​​​ALTER TABLE Student
DROP COLUMN Mark;


1) SQL Server / MS Access
ALTER TABLE table_name
ALTER COLUMN column_name datatype


Example
ALTER TABLE Student
ALTER COLUMN Total Varchar(255);


2) My SQL / Oracle (prior version 10G)
ALTER TABLE table_name
MODIFY COLUMN column_name datatype


Example
ALTER TABLE Student
MODIFY COLUMN Total Varchar(255);


3) Oracle 10G and later
ALTER TABLE table_name
MODIFY column_name datatype


Example
​​​​​​​ALTER TABLE Student
MODIFY Total Varchar(255);


TRUNCATE
Truncate command helps to remove all records from a table.
TRUNCATE TABLE  table_name


Example
TRUNCATE TABLE  Student;

COMMENT
Comment is helpful to add comments to the data dictionary."--" is used to comment the notes.
--(notes,examples)

Example
--select the student data
SELECT * FROM Student;


RENAME

Rename is helpful to rename an object existing in the database.
1) PostgreSQL
ALTER DATABASE "Old_DatabaseName" RENAME TO "New_DatabaseName";

Example
ALTER DATABASE "Student_data" RENAME TO "Employee_data";

2) MySQL
Example
SQL Command for Dump copy
mysqldump -u username -p"password" -R testDb > testDb.sql;

SQL Command for creating new DB
mysqladmin -u username -p"password" create testDB1;

SQL Command for Import
mysql -u username -p"password" testDb1 < testDb.sql;

Also for Unix, database names are case-sensitive

3) SQL Server
In SQL Server we can rename the database through server application, by right click the existing database and renaming it.

DQL (Data Query Language)
DQL or data query language is to perform the query on the data inside the schema or object (ie table, index, view, function, etc). With the help of DQL query we can get the data from the database to perform actions or operations like analysing the data.

SELECT
Select query on a table or tables to view the temporary table output from the database.
Select * from Table_Name;

Example
Select * from Student;

DML(Data Manipulation Language)
DML or Data Manipulation Language is to manipulate the data inside the database. With the help of DML commands, we can insert, delete, change the data inside the database.

INSERT
Insert command is helpful to insert the data into a table.

1) All the column names are mentioned in the insert statement.
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...)


Example
INSERT INTO Student (StudendId, FirstName, LastName)
VALUES (12345, "Sri", "Durga");

2) Column names do not need to mentioned in the query, Values should be given in the order according to the column.
INSERT INTO table_name
VALUES (value1, value2, value3, ...)

Example
INSERT INTO Student
VALUES (12345, "Anna", "Marry");

UPDATE
Update command is helpful to update the existing data in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition


Example
UPDATE Student
SET FirstName = "Peter" , LastName = "Scott"
WHERE StudentId=12345;


DELETE
Delete command helps to delete the records from a database table.

Syntax
DELETE FROM table_name WHERE condition;

Example
DELETE FROM Student WHERE StudentId=12345;

LOCK
Lock command is helpful to lock the table to control concurrency.
LOCK TABLE table-Name IN { SHARE | EXCLUSIVE } MODE

Example
LOCK TABLE Student IN SHARE MODE;

CALL
Call command is helping to Call a PL/SQL or JAVA subprogram.
EXEC SQL
 CALL GETEMPSVR (2, NULL)
END-EXEC


EXPLAIN PLAN
It describes the access path to the data.
EXPLAIN PLAN FOR
SELECT Column_name FROM table_name


Example
EXPLAIN PLAN FOR
SELECT last_name FROM Student;


SQL
This query explanation will be stored in the PLAN_TABLE table. We can then select the execution plan to review the queries.
DCL (Data Control Language)

DCL or Data Control Language is to provide rights, permissions, and other controls of the database system.

GRANT
GRANT command is helpful to provide privileges to the database.
GRANT privileges_names ON object TO user

Example
GRANT SELECT, INSERT, DELETE, UPDATE ON Users TO 'Name'@'localhost;
GRANT ALL ON Users TO 'Name'@'localhost;
GRANT SELECT  ON Users TO '*'@'localhost;


GRANT EXECUTE ON [ PROCEDURE | FUNCTION ] object TO user

REVOKE
Revoke command is to withdraw the user’s access privileges given by using the GRANT command.

REVOKE privileges ON object FROM user

Example
REVOKE SELECT, INSERT, DELETE, UPDATE ON Users TO 'Name'@'localhost;

REVOKE ALL ON Users TO 'Name'@'localhost;

REVOKE SELECT  ON Users TO '*'@'localhost;

REVOKE EXECUTE ON [ PROCEDURE | FUNCTION ] object FROM user
TCL (Transaction Control Language)


TCL or Transaction Control Language happens to a transaction in the database.

COMMIT
Commit command is to commit Transaction after insert or delete in the database.
Commit;

Example
DELETE from Student where Student_Id = 12345;
COMMIT;

ROLLBACK
Rollback command is to rollback a transaction in case of any error occurs.
Rollback;

Example
DELETE from Student where Student_Id = 12345;
ROLLBACK;


SAVEPOINT
Savepoint command is to Set a savepoint within a transaction. If transaction happens in big data, then for checking and rollup can't do it with all the data, to rollback the small part of the data we use savepoint query.

SAVEPOINT savepoint_name

SET TRANSACTION
Set command is to Specify the characteristics of the transaction.

SET TRANSACTION Access NAME transaction_name



SQL Server Hosting - HostForLIFE :: Convert SQL Server Data to JSON

clock March 9, 2022 07:31 by author Peter

In this article, let’s learn how to convert SQL Server data to JSON format. JSON format has become a standard way to represent data objects into strings. JSON format is commonly used in APIs to trasfer data from one application to other via APIs.

You can convert a SQL query results in JSON format in SQL Server by simply adding FOR JASON clause to the query. FOR JASON is used with PATH and AUTO
SELECT name, surname
FROM emp
FOR JSON AUTO;

Here is a simple SQL query on Northwind database that returns 10 orders from the Orders table.
SELECT TOP (10) [OrderID]
      ,[OrderDate]
      ,[ShipName]
      ,[ShipAddress]
      ,[ShipCity]
      ,[ShipPostalCode]
      ,[ShipCountry]
  FROM [Northwind].[dbo].[Orders]

The output in SSMS looks like this.

Now, let’s add FOR JASON PATH clause at the end of the SQL query.
SELECT TOP (10) [OrderID]
      ,[OrderDate]
      ,[ShipName]
      ,[ShipAddress]
      ,[ShipCity]
      ,[ShipPostalCode]
      ,[ShipCountry]
  FROM [Northwind].[dbo].[Orders]
  FOR JSON PATH;


The new output looks like this that is a JSON object.
[{"OrderID":10248,"OrderDate":"1996-07-04T00:00:00","ShipName":"Vins et alcools Chevalier","ShipAddress":"59 rue de l'Abbaye","ShipCity":"Reims","ShipPostalCode":"51100","ShipCountry":"France"},{"OrderID":10249,"OrderDate":"1996-07-05T00:00:00","ShipName":"Toms Spezialitäten","ShipAddress":"Luisenstr. 48","ShipCity":"Münster","ShipPostalCode":"44087","ShipCountry":"Germany"},{"OrderID":10250,"OrderDate":"1996-07-08T00:00:00","ShipName":"Hanari Carnes","ShipAddress":"Rua do Paço, 67","ShipCity":"Rio de Janeiro","ShipPostalCode":"05454-876","ShipCountry":"Brazil"},{"OrderID":10251,"OrderDate":"1996-07-08T00:00:00","ShipName":"Victuailles en stock","ShipAddress":"2, rue du Commerce","ShipCity":"Lyon","ShipPostalCode":"69004","ShipCountry":"France"},{"OrderID":10252,"OrderDate":"1996-07-09T00:00:00","ShipName":"Suprêmes délices","ShipAddress":"Boulevard Tirou, 255","ShipCity":"Charleroi","ShipPostalCode":"B-6000","ShipCountry":"Belgium"},{"OrderID":10253,"OrderDate":"1996-07-10T00:00:00","ShipName":"Hanari Carnes","ShipAddress":"Rua do Paço, 67","ShipCity":"Rio de Janeiro","ShipPostalCode":"05454-876","ShipCountry":"Brazil"},{"OrderID":10254,"OrderDate":"1996-07-11T00:00:00","ShipName":"Chop-suey Chinese","ShipAddress":"Hauptstr. 31","ShipCity":"Bern","ShipPostalCode":"3012","ShipCountry":"Switzerland"},{"OrderID":10255,"OrderDate":"1996-07-12T00:00:00","ShipName":"Richter Supermarkt","ShipAddress":"Starenweg 5","ShipCity":"Genève","ShipPostalCode":"1204","ShipCountry":"Switzerland"},{"OrderID":10256,"OrderDate":"1996-07-15T00:00:00","ShipName":"Wellington Importadora","ShipAddress":"Rua do Mercado, 12","ShipCity":"Resende","ShipPostalCode":"08737-363","ShipCountry":"Brazil"},{"OrderID":10257,"OrderDate":"1996-07-16T00:00:00","ShipName":"HILARION-Abastos","ShipAddress":"Carrera 22 con Ave. Carlos Soublette #8-35","ShipCity":"San Cristóbal","ShipPostalCode":"5022","ShipCountry":"Venezuela"}]

Now, you can use this same return value from SQL query in your application to read JSON objects in your code.

Using the same method, you can convert a SQL Server Table to JSON by using a SELECT * or SELECT column names query on the entire table. The following SQL query converts all rows of a SQL Server table to a JSON string.
SELECT [OrderID]
      ,[OrderDate]
      ,[ShipName]
      ,[ShipAddress]
      ,[ShipCity]
      ,[ShipPostalCode]
      ,[ShipCountry]
  FROM [Northwind].[dbo].[Orders]
  FOR JSON PATH;


Here is a detailed article on JSON in SQL Server with various options.

HostForLIFEASP.NET SQL Server 2019 Hosting

 



SQL Server Hosting - HostForLIFE :: SSIS Expression - Convert Date To String

clock March 8, 2022 07:47 by author Peter

SQL Server Integration Services (SSIS) is a component of the Microsoft SQL Server database software that can be used to perform a broad range of data migration tasks. SQL Server Integration Services (SSIS) expressions can help make SSIS packages more dynamic.

In general, expressions in SSIS is a combination of literals, functions, operators that yields a single data value.

An expression can be composed of a single value (“abc”) or a variable (@[User::FilePath]) or a function (GETDATE()), or it can be more complex and contains some conditionals (CASE statement) or some mathematical operations or some string manipulation.

Here I am describing how we can convert the datetime into string with SSIS expression.

There are multiple ways to convert date to string. The two ways are listed below.
(DT_WSTR, 4)(YEAR(@[User::PositionDateToProcess])) + "-"+
(DT_WSTR, 2)(MONTH(@[User::PositionDateToProcess])) +"-"+
(DT_WSTR, 2)(DAY(@[User::PositionDateToProcess]))


2. SUBSTRING((DT_STR,30, 1252) GETDATE(), 1, 10)

HostForLIFEASP.NET SQL Server 2019 Hosting



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Month List

Tag cloud

Sign in