CREATE TABLE "maintable"( "ID" INT NOT NULL IDENTITY (1, 1) PRIMARY KEY, "name" NVARCHAR (50) NULL, "description" NVARCHAR (100) NULL ) CREATE TABLE "secondtable" ( "ID" INT NOT NULL IDENTITY (1, 1) PRIMARY KEY, "sname" NVARCHAR (50) NULL, "sdescr" NVARCHAR (100) NULL, "somefirstnumber" INT DEFAULT 0, "somesecondnumber" INT DEFAULT 1, "somedatetime" DATETIME NOT NULL, "howmanyhours" INT NULL DEFAULT 0, "newdate" AS DATEADD(HOUR, "howmanyhours", "somedatetime"), "mainID" INT NOT NULL CONSTRAINT FK_SecondToMain FOREIGN KEY ("mainID") REFERENCES "maintable" ("ID") ); -- CREATE PROCEDURE "insertintomain" @Name NVARCHAR (50), @Descr NVARCHAR (100) AS INSERT INTO "SimpleIndex"."dbo"."maintable" ("name", "description") VALUES (@Name, @Descr); GO CREATE PROCEDURE "insertintosecond" @Sname NVARCHAR(50), @Sdescr NVARCHAR(100), @Firstnumber INT, @Secondnumber INT, @SomeDT DATETIME, @Hours INT, @MainID INT AS INSERT INTO "SimpleIndex"."dbo"."secondtable" ( "sname", "sdescr", "somefirstnumber", "somesecondnumber", "somedatetime", "howmanyhours", "mainID") VALUES ( @Sname, @Sdescr,@Firstnumber, @Secondnumber, @SomeDT, @Hours, @MainID) GO
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace SimpleAddingRandomRecordsToDatabase { class Program { static string alphabet() { // string alphabet = "abcdefgh ijklmnopqrst uvwxyz "; // Encoding enc = Encoding.UTF8; var bytes = enc.GetBytes(alphabet); alphabet = enc.GetString(bytes); return alphabet; } static int alphlen = alphabet().Length; static string getRandomString(int lenth, int rndparam, bool allowspacebars) { string str = ""; Random rand = new Random(rndparam); for (int i = 0; i < lenth; i++) { int num = rand.Next(alphlen); str = str + alphabet()[num]; } if (!allowspacebars) { str = str.Replace(" ", ""); } return str; } static void Main(string[] args) { string result; Console.WriteLine(" 1 "); result = Console.ReadLine(); if (result.Equals("1")) { // SqlConnection cn = new SqlConnection(); SqlConnectionStringBuilder cs = new SqlConnectionStringBuilder(); cs.IntegratedSecurity = true; cs.DataSource = @"KONSTANTIN\KONSTANTINSQL"; cs.InitialCatalog = "SimpleIndex"; cn.ConnectionString = cs.ToString(); cn.Open(); // 10 000 int i2 = 0; for (int i = 0; i < 10000; i++) { SqlCommand cm = cn.CreateCommand(); cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "insertintomain"; i2++; cm.Parameters.Add(new SqlParameter("Name", getRandomString(50, i2, false))); i2++; cm.Parameters.Add(new SqlParameter("Descr", getRandomString(100, i2, true))); cm.ExecuteNonQuery(); } // 1 000 000 , i2 = 0; for (int i = 0; i < 1000000; i++) { Random rand = new Random(); DateTime dt = DateTime.Now; dt = dt.AddHours((-1) * rand.Next(2000)); SqlCommand cm = cn.CreateCommand(); cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "insertintosecond"; i2++; cm.Parameters.Add(new SqlParameter("Sname", getRandomString(50, i2, false))); i2++; cm.Parameters.Add(new SqlParameter("Sdescr", getRandomString(100, i2, true))); i2++; Random rand2 = new Random(i2); cm.Parameters.Add(new SqlParameter("Firstnumber", rand2.Next(10000))); i2++; rand2 = new Random(i2); cm.Parameters.Add(new SqlParameter("Secondnumber", rand2.Next(100))); cm.Parameters.Add(new SqlParameter("SomeDT", dt)); i2++; rand = new Random(i2); cm.Parameters.Add(new SqlParameter("Hours", rand.Next(30))); i2++; rand = new Random(i2); cm.Parameters.Add(new SqlParameter("MainID", rand.Next(9999)+1)); cm.ExecuteNonQuery(); } cn.Close(); Console.WriteLine(" ."); } } } }
SELECT "name", "description" FROM "maintable" WHERE "ID" = 3823;
Time spent searching: 1,523 seconds . SELECT "name", "description" FROM "maintable" WHERE "ID" BETWEEN 5000 AND 6000;
The first request took 0.3 seconds , the second 0.26 seconds , the third 0.25 seconds . SELECT * FROM "secondtable" WHERE "ID" = 728472;
Time spent on search: 1.4 seconds , the second query showed 1.36 seconds. . Next will be the average request time. SELECT * FROM "maintable" WHERE "name" LIKE '%';
Without an index, such a query is executed in 0.172 seconds out of 10,000 rows. With index CREATE NONCLUSTERED INDEX iName ON "maintable" ("name")
this is done in 0.112 seconds , and with a composite index containing the “name” and “description” fields, the request took 0.09 seconds . SELECT * FROM "maintable" WHERE "name" LIKE '%dc%';
runs in 0.172 seconds with no index and 0.112 seconds with an index . SELECT * FROM "secondtable" WHERE "sname" LIKE '%';
This query without indexing would take an average of 1.7 seconds , whereas with a column index it would take 0.15 seconds . SELECT * FROM "secondtable" WHERE "somedatetime" BETWEEN '2012-26-11 11:30:00' AND '2012-26-11 11:32:00'
Without an index, it takes an average of 0.7 seconds , indexed - 0.22 seconds . SELECT * FROM "secondtable" WHERE "somedatetime" BETWEEN '2012-26-11 11:30:00' AND '2012-31-12 11:32:00'
Without an index - 19.219 seconds , with an index of the date field only - 9.9 seconds . SELECT * FROM "secondtable" WHERE "newdate" BETWEEN '2012-26-11 11:30:00' AND '2012-31-12 11:32:00'
This query without an index will take 12.2 seconds , whereas with an index - 9.75 . It is important to note that the original column containing the date has already been indexed. CREATE NONCLUSTERED INDEX iFK ON "secondtable" ("mainID"); SELECT "name", "sname", "somefirstnumber" FROM "maintable" M INNER JOIN "secondtable" S ON M."ID" = S."mainID" WHERE M."ID" = 8271
Such a request returns 101 lines. Without an index - 0.422 seconds , and with an index - 0.122 seconds . As we see, the query speed has increased more than three times. Consider the remaining queries related to the merge. SELECT "name", "sname", "somefirstnumber" FROM "maintable" M INNER JOIN "secondtable" S ON M."ID" = S."mainID" WHERE S."somedatetime" BETWEEN '2012-26-11 11:30:00' AND '2012-31-12 11:32:00';
showed identical results without an index, with an index of only a foreign key, a foreign key and a date, with a composite index simultaneously including these two columns - approximately 6.8 seconds . SELECT "name", "sname", "somefirstnumber" FROM "maintable" M INNER JOIN "secondtable" S ON M."ID" = S."mainID" WHERE "sname" LIKE '%';
Without indices, the query will take 0.766 seconds and return 5 rows.Source: https://habr.com/ru/post/164717/
All Articles