Indexing a database - sql

I want to index a db. But in most of the table foreign key constraints are not set.So before start indexing whether we have to set the foreign key for all tables? Or just index the table columns according to the queries.Please give your valuable suggestions.
Mahesh

Short Answer: Indexes do not rely on foreign keys or their constraints, foreign keys can enhanced by indexes though.. but indexes are often placed on columns that are NOT foreign keys.
Little detail: You should look at what you are searching for within your tables, and create indexes on those columns - but take that step seriously, indexing is a valuable process in database design, but can hurt you if done incorrectly.

Yes: Create Foreign Keys where the Domain requires them (i.e. all relationships to PK's). Even without indexes the optimiser will take these into account.
You can start by defining indexes on all FK columns, but it would be better to capture a representative query workload to determine your most expensive queries, and then index accordingly.

Related

Indexes In Postgres

So i am trying to wrap my head around what indexes in postgresql, I have understood so far that indexes help for faster querying and that all primary keys have an index, I was wondering if all foreign keys(referring to a primary key of another table) should also have an index or is it just the attributes in a table that we are querying that need indexes?
Thanks.
Indexes should be based primarily on queries that are being used. If you are going to be filtering or aggregating or sorting by the foreign key, then it is sensible as an index.
The other use is using indexes to enforce unique constraints.
In other words, there is no rule that foreign keys should always be indexed. It is often a good idea, but that depends on the queries you want to run.
It is not mandatory to index the foreign key. But if you ever delete from the referenced table, or update it in a way that changes the primary key, then it will have to search for rows in the referencing table by the foreign key column to make sure it will be left with no violations (or to set them to null, or delete them, depending on what action was defined in the foreign key). Without an index on the foreign key column this will likely be slow.
Also, it is likely you will want to run queries like "Show me all the detail rows of this master row", and without a index that will also be slow.
If you never delete from the referenced table and never run that kind of query, then you probably do not need the index.

In PostgreSQL what tables with no primary key used for

I've read a lot about this issue.. (also read this: Tables with no Primary Key)
it seems like there is no reason to use tables with no PK. so why does PostgreSQL allows it? can you give an example when it's good idea to not indicate PK?
I think the answer to your question lies in trying to understand what are the drawbacks of having a Primary-Key (PK) in the first place.
One obvious 'drawback' (depending on how you see it) in maintaining a PK is that it has its own overhead during an INSERT. So, in order to increase INSERT performance (assuming for e.g. the sample case is a logging table, where Querying is done offline) I would remove all Constraints / PK if possible and definitely would increase table performance. You may argue that pure logging should be done outside the DB (in a noSQL DB such as Cassandra etc.) but then again at least its possible in PostgreSQL.
A primary key is a special form of a unique constraint. A unique constraint is always backed up by an index. And the disadvantage of an index is that it takes time to update. Tables with an index have lower update, delete and insert performance.
So if you have a table that has a lot of modifications, and few queries, you can improve performance by omitting the primary key.
AFAIK, the primary key is primarily needed for the relationships between tables as a foreign key. If you have a table that is not linked to anything you don't need a primary key. In Excel spreadsheets there're no primary keys but a spreadsheet is not a relational database.

Create an index on columns

We have some tables, in which we didn't give any foreign key constraints. So if we indexing those columns without giving foreign key constraints, is there any performance issue?
Indexes will increase your performance only if you are using the indexed columns in your queries. So index a column only if you are using that column frequently in the queries. The foreign key constraint has no relation for the column to be indexed.
Not defining a foreign key constraint on a column which is supposedly acting as foreign-key in your DB, then you will probably face data-integrity issues as already mentioned by #The scrum.
Depends on the query optimizer. Optimizers can make certain assumptions when they know about foreign key constraints. There used to be a bug in SQL Server's optimizer related to those assumptions.
But query performance isn't the real issue. Data integrity is the real issue.
Is it important to get the wrong answer really, really fast?

Many-to-many link table design : two foreign keys only or an additional primary key?

this is undoubtedly a newbie question, but I haven't been able
to find a satisfactory answer.
When creating a link table for many-to-many relationships, is it better to
create a unique id or only use two foreign keys of the respective tables (compound key?).
Looking at different diagrams of the Northwind database for example, I've come across
both 'versions'.
That is: a OrderDetails table with fkProductID and fkOrderID and also versions
with an added OrderDetailsID.
What's the difference? (does it also depend on the DB engine?).
What are the SQL (or Linq) advantages/disadvantages?
Thanks in advance for an explanation.
Tom
ORMs have been mandating the use of non-composite primary keys to simplify queries...
But it Makes Queries Easier...
At first glance, it makes deleting or updating a specific order/etc easier - until you realize that you need to know the applicable id value first. If you have to search for that id value based on an orders specifics then you'd have been better off using the criteria directly in the first place.
But Composite keys are Complex...
In this example, a primary key constraint will ensure that the two columns--fkProductID and fkOrderID--will be unique and indexed (most DBs these days automatically index primary keys if the clustered index doesn't already exist) using the best index possible for the table.
The lone primary key approach means the OrderDetailsID is indexed with the best index for the table (SQL Server & MySQL call them clustered indexes, to Oracle they're all just indexes), and requires an additional composite unique constraint/index. Some databases might require additional indexing beyond the unique constraint... So this makes the data model more involved/complex, and for no benefit:
Some databases, like MySQL, put a limit on the amount of space you can use for indexes.
the primary key is getting the most ideal index yet the value has no relevance to the data in the table, so making use of the index related to the primary key will be seldom if ever.
Conclusion
I don't see the benefit in a single column primary key over a composite primary key. More work for additional overhead with no net benefit...
I'm used to use PrimaryKey column. It's because the primary key uniquely identify the record.
If you have a cascade-update settings on table relations, the values of foreign keys can be changed between "SELECT" and "UPDATE/DELETE" commands sent from application.

SQL Server and indices

Is it beneficial to add an index to a column that is part of a foreign key relationship? I have two columns which will be queried frequently and already have them foreign keyed but wasn't sure if I should index them aswell, or if the foreign key creates an index behind the scenes?
SQL Server does not create a behind the scenes index, so creating an index on all foreign key fields is advisable to improve look up performance.
Details and additional benefits: http://technet.microsoft.com/en-us/library/ms175464.aspx
It is defenitely advisable to add an index to your FK column if you query it often.
In your situation, it is probably even better if you create a composite index which spans your 2 columns.
This is only advisable however (composite index) , if you often execute queries that filter / order on these 2 columns.
If you decide that a composite index is appropriate, then you should pay attention to the order in which you put an index on those columns.
Creating an index for your FK fields is strongly adviseable - this will almost definitely improve performance. Note that's for selects, inserts may be slower as the index itself will also have to be updated.
Sql Server will sometimes create an FK index 'on the fly' but that only exists for the lifetime of your query - you can look at the Sql Execution plan to see if that is occuring. Either way, creating your own index, under your control for FK fields is the way to go.