Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Friday, 29 April 2016

SQL VIEWS


A quick note on SQL views.  “In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.” – this is the definition of a SQL view.  I wish more people would realise the importance of this definition.  It essentially means that, although the view in question may seem like a table when queried through Management Services or something similar, it is just that – it is a result of a SQL query statement.

Too many times I have come across databases that are almost entirely built with views because the person building the queries does not realise this fundamentally important principle.  Don’t get me wrong – views have their place in many databases, especially smaller ones where performance is not a major concern.  The danger is when the database continues to grow and develop and the developer / perpetrator does not change the way the data is being presented.  You will then find that you have a SQL statement that takes longer and longer to run.  The reason? You develop a query that produces the resulting data that you were asked to provide and this queries a view.  In turn, this view may query more views which may also query more, ad infinitum.  Nested queries can be a ticking time bomb if not kept in check and the dangers become apparent very quickly as time goes on. 

If you want a report or query to run properly, my advice is to get rid of your views.  There should never be a complex query run on a view and there are NO exceptions.  Don’t listen to anyone who says different.  These are people who avoid and fall in the realm of Einstein’s advice – “if you can’t explain it simply, you don’t understand it well enough”.  I’ve met many people like this in my time and the only advice worth giving about them is to stay well clear.  This type of person will not help you in your endeavours.

So, my advice?  Use views sparingly and respect your users and servers. 

Monday, 14 March 2016

SQL – Joins

SQL joins are used to combine rows from more than one table.  I will be setting up a code page and will provide a link when this has been completed.
The different JOINs are as follows:
LEFT (or LEFT OUTER) JOIN: returns all rows from the first or left hand side table, even if there are no matches in the right table.  This is the main JOIN used when querying SQL.
INNER JOIN: returns rows when there is a match in both tables - produces a result set that is limited to the rows where there is a match in both tables for what you are looking for.
RIGHT (or RIGHT OUTER) JOIN: returns all rows from the right table, even if there are no matches in the left table.
FULL (or FULL OUTER) JOIN: returns rows when there is a match in one of the tables.  All rows from both tables are returned.
CROSS JOIN: returns a table with a potentially very large number of rows.  The row count of the result is equal to the number of rows in the first table times the number of rows in the second table.
SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN: no-one uses this – see CROSS JOIN.  Returns the Cartesian product of the sets of records from the two or more joined tables.




Sunday, 13 March 2016

SQL - Indexes

Indexes are special lookup tables that the database search engine can use to speed up searches and queries. An index in a database is very similar to an index in the back of a book.  It is used to efficiently find all rows matching a column in your query and then walk through that subset of the table for exact matches.  If you don’t have indexes then every row in the table will be checked.
An index will speed up your SELECT queries but will slow down data input, i.e. when you use UPDATE or INSERT statements.
Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify the table and which column or columns to index, and to indicate whether the index is in ascending or descending order.
Whatever index you create, take into consideration the column(s) that you may use very frequently in a query's WHERE clause as filter conditions.
Single-Column
A single-column index is one that is created based on only one table column. 
Unique Index
The index can be unique, whereby you cannot have duplicate values in that column, or a Primary Key which in some storage engines defines where in the database file the value is stored.  Creating a unique index “provides additional information for the query optimizer that can produce more efficient execution plans”, as per SQL Server Index Design Guide.
Composite Index
A composite index is an index on two or more columns of a table.
Implicit Index
Implicit indexes are indexes that are automatically created by the database server when an object is created. Indexes are automatically created for primary key constraints and unique constraints.
Hash Table Index
Hash tables are another data structure that can be used as indexes - known as hash indexes. Hash indexes are used because hash tables are extremely efficient when it comes to just looking up values. The only thing you must keep in mind is that hash table is only good for looking up key value pairs, i.e. equality rather than a range of values. 
 A table that does not have a clustered index is referred to as a HEAP and a table that has a clustered index is referred to as a clustered table (although a HEAP table can have a non-clustered index – but that’s for another time).