Create an index on columns - sql

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?

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.

Indexing a database

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.

Why use primary keys?

What are primary keys used aside from identifying a unique column in a table? Couldn't this be done by simply using an autoincrement constraint on a column? I understand that PK and FK are used to relate different tables, but can't this be done by just using join?
Basically what is the database doing to improve performance when joining using primary keys?
Mostly for referential integrity with foreign keys,, When you have a PK it will also create an index behind the scenes and this way you don't need table scans when looking up values
RDBMS providers are usually optimized to work with tables that have primary keys. Most store statistics which helps optimize query plans. These statistics are very important to performance especially on larger tables and they are not going to work the same without primary keys, and you end up getting unpredictable query response times.
Most database best practices books suggest creating all tables with a primary key with no exceptions, it would be wise to follow this practice. Not many things say junior software dev more than one who builds a database without referential integrity!
Some PKs are simply an auto-incremented column. Also, you typically join USING the PK and FK. There has to be some relationship to do a join. Additionally, most DBMS automatically index PKs by default, which improves join performance as well as querying for a particular record based on ID.
You can join without a primary key within a query, however, you must have a primary key defined to enforce data integrity constraints, at least with SQL Server. (Foreign Keys, etc..)
Also, here is an interesting read for you on Primary Keys.
In Microsoft Access, if you have a linked table to, say, SQL Server, the source table must have a primary key in order for the linked table to be writeable. At least, that was the case with Access 2000 and SQL Server 6.5. It may be different with later versions.
Keys are about data integrity as well as identification. The uniqueness of a key is guaranteed by having a constraint in the database to keep out "bad" data that would otherwise violate the key. The fact that data integrity rules are guaranteed in that way is precisely what makes a key usable as an identifier. That goes for any key. One key per table by convention is called a "primary" key but that doesn't make other alternate keys any less important.
In practice we need to be able to enforce uniqueness rules against all types of data (not just numbers) to satisfy the demands of data quality and usability.

SQL Server Foreign Key constraint benefits

We're designing a database in which I need to consider some FK(foreign key)
constraints. But it is not limited to
formal structuring and normalization.
We go for it only if it provides any
performance or scalability benefits.
I've been going thru some interesting articles and googling for practical benefits. Here are some links:
http://www.mssqltips.com/tip.asp?tip=1296
I wanted to know more about the benefits of FK (apart from the formal structuring and the famous cascaded delete\update).
FK are not 'indexed' by default so what are the considerations while indexing an FK?
How to handle nullable fields which are mapped as foreign key - is this allowed?
Apart from indexing, does this help in optimizing query-execution plans in SQL-Server?
I know there's more but I'd prefer experts speaking on this. Please guide me.
Foreign keys provide no performance or scalability benefits.
Foreign keys enforce referential integrity. This can provide a practical benefit by raising an error if someone attempted to delete rows from the parent table in error.
Foreign keys are not indexed by default. You should index your foreign keys columns, as this avoids a table scan on the child table when you delete/update your parent row.
You can make a foreign key column nullable and insert null.
The main benefit is that your database will not end up inconsistent if your buggy client code tries to do something wrong. Foreign keys are a type of 'constraint', so that's how you should use them.
They do not have any "functional" benefit, they will not optimize anything. You still have to create indexes yourself, etc. And yes, you can have NULL values in a column that is a foreign key.
FK constraints keep your data consistent. That's it. This is the main benefit.
FK constraints will not provide you with any performance gain.
But, unless you have denormalized on purpose db structure, I'd recommend you to use FK constraints. The main reason - consistency.
I have read at least one example on net where it was shown that Foreign Keys do improve performance because the optimiser does not have to do additional checks across tables because it knows data meets certain criteria already due to the FK. Sorry I don't have a link but the blog gave detailed output of the query plans to prove it.
As mentioned, they are for data integrity. Any performance "loss" would be utterly wiped out by the time required to fix broken data.
However, there could be an indirect performance benefit.
For SQL Server at least, the columns in the FK must have the same datatype on each side. Without an FK, you could have an nvarchar parent and a varchar child for example. When you join the 2 tables, you'll get a datatype conversions which can kill performance.
Example: different varchar lengths causing an issue