sql server partitioned table related indexes - partitioning

I have a huge table which contains around 3B records.
this table contains 3 indexes :
1 clustered index on date and ID fields
1 non-clustered index on date
1 non-clustered index on ID
I want to transform this table into a partitioned table based on date field (each partition will have 4 hours)
the idea of partitioned table is clear , but I'm confused regarding the indexes.
can I keep the same indexes ? do I have to create partitioned indexes ?
All hints are appreciated.

can I keep the same indexes ?
Yes, you can. You can create indexes on partitioned table the same way. In fact every table is partitioned - non-partitioned tables are actually tables with a single partition.
do I have to create partitioned indexes ?
No, you don't have to. But it is highly recommended to align your table and its indexes, otherwise you will lose the advantages of having partitioned table, like swapping partitions. When a table and its indexes are in alignment, SQL Server can switch partitions quickly and efficiently while maintaining the partition structure of both the table and its indexes.

Related

Should I setup index on a table having 15k -30k row?

I have a table we will be of 15-30k in size eventually not more.
I have only two columns in that table one is id and other is status
We will have insert queries ofc
We will have update query but not on id
We will have delete query
My question is should I create index on the column id ?
Will index be useful for a table having 15k-30k rows ? Or will it be negligible ?
Also I am concerned about the increase in cost of insert queries? Will it be worth to add index on id ? Considering the small table size will it be any faster or the effect will be negligible ?
If effect will be negligible, I should not add index to it right as it will increase the insert queries cost right ?
If your id column is a PRIMARY KEY, then it's already an index and there is no need to create a new one.
If no primary key is defined, it's best to get used of creating one for each table.
Without any index, MySQL has to start with the first row and go through the whole table to find the relevant rows.
Indexes make it possible to find data much faster, even on tables with few data.
MySQL 8.0 Reference Manual:
https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html

Does an index create a new table?

I’m having trouble understanding exactly what happens when I create an index. So if I create an index on (Col1, Col2). Will there be a new table created containing only Col1 and Col2 which my query will run on?
Or do I specify that I’m using an index in my query and then for that query and that query only my table is now reduced to (Col1, Col2)?
Thanks
When you create an index on a table, internally it doesn't create any new table but just index. This index is physically separate and has data organized like a tree (usually B+ tree) which helps in faster lookups and speeds up the queries that have where clause having the column name on which index are created. If you don't create indexes on table then the queries on that table may have to do full table scan in order to find records. Indexes can be unique or non unique.

What happens if I put index on each column in a table

Let us consider I have a table with 60 columns , I need to perform all kind of queries on that table and need to join that table with other tables as well. And I almost using all rows for searching data in that table including other tables. This table is the like a primary table(like a primary key) in the database. So all table are in relation with this table.
By considering the above scenario can I create index on each column on the table (60 columns )
,is it good practice ?
In single sentence:
Is it best practice to create index on each column in a table ?
What might happens if I create index on each column in a table?
Where index might be "Primary key", "unique key" or "index"
Please comment, if this question is unclear for you people I will try to improve this question.
MySQL's documentation is pretty clear on this (in summary use indices on columns you will use in WHERE, JOIN, and aggregation functions).
Therefore there is nothing inherently wrong with creating an index on all columns in a table, even if it is 60 columns. The more indices there are the slower inserts and some updates will be because MySQL has to create the keys, but if you don't create the indices MySQL has to scan the entire table if only non-indexed columns are used in comparisons and joins.
I have to say that I'm astonished that you would
Have a table with 60 columns
Have all of those columns used either in a JOIN or WHERE clause without dependency on any other column in the same table
...but that's a separate issue.
It is not best practice to create index on each column in a table.
Indexes are most commonly used to improve query performance when the column is used in a where clause.
Suppose you use this query a lot:
select * from tablewith60cols where col10 = 'xx';
then it would be useful to have an index on col10.
Note that primary keys by default have an index on them, so when you join the table with other tables you should use the primary key to join.
Adding an index means that the database has to maintain it, that means that it has to be updated, so the more writes you have, the more the index will be updated.
Creating index out of the box is not a good idea, create an index only when you need it (or when you can see the need in the future... only if it is pretty obvious)
creating more index in SQL will increase only search speed while you will get slowness of insert and update and also it will take more storage.

best practices for table partitioning - mysql 5.5

I have a mysql 5.5 server with a big (~150M records) innodb table that I want to partition.
How can I determine the optimal number of partitions for the table?
The table is a "many-to-many" connection table that consists of only two int columns (aId, bId),
aId receives values roughly on the range of 1..1,000,000
bId receives values roughly on the range of 1..10,000,000
Most queries lookup aId first.
the table has two indexes:
primary(aId, bId)
index(bId)
And, again, the question is How can I determine the optimal number of partitions for the table?
Thanks
If you do not want to structure your data logically, then optimal number of partitions is the same as number of cores on the server. This will aid multiple concurrent queries. Individual queries will run the same time due to using primary index.
I would suggest changing your second index to index (bld,ald), so bld to ald queries can use only index to get ald value, without going to the database for it.

MySQL Create table with two indexes on same column(s)

Is it possible to create two indexes with names different on the same column?
Yes, you can, but why would you do that?
Unless the indexes are different in some way, for example if there are additional columns, or differences in the order of the columns in the indexes, a second duplicated index would be redundant.
Each additional index on a table requires more disk storage (slight cost increase), and also means more data needs to be written when inserting, updating or deleting data (slightly slower writes).
But yes, it is possible, and the syntax is one would expect, e.g. given the table:
CREATE TABLE T1
(
col1 INT,
col2 INT
);
CREATE INDEX IX1 on T1(col1);
CREATE INDEX IX2 on T1(col1);
SQL Fiddle here