Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL analog of SQL Server index(include columns)

Trying to recreate my SQL Server database on PostgreSQL. Everything is ok except I can't find how to recreate this index:

USE [mytablename]  
GO  
CREATE NONCLUSTERED INDEX [myindex]  
ON [dbo].[mytablename] ([col1],[col2])  
INCLUDE ([col3],[col4])  
GO  

Will be very grateful for help.

Alexey

Update:

http://img38.imageshack.us/img38/1071/89013974.png here is db structure star+eav
there is only one query

SELECT this_.id as id0_0_,   
this_.device_id as device2_0_0_,  
this_.time_id as time3_0_0_,  
this_.gps_detail_id as gps4_0_0_   
FROM [scoutserver_data].[dbo].[DataMessage]  this_   
WHERE this_.time_id = 65536 and this_.device_id = 32768  

Maybe it is not optimal atm. And im working on it also. Maybe something like this

SELECT * FROM [scoutserver_data].[dbo].[TimeDimension]   
  INNER JOIN ([scoutserver_data].[dbo].[DeviceDimension]   
  INNER JOIN  [scoutserver_data].[dbo].[DataMessage]   
ON [DeviceDimension].[device_id] =[DataMessage].[device_id])  
ON [TimeDimension].[time_id] = [DataMessage].[time_id]  
WHERE DeviceDimension.serial_id='2' AND TimeDimension.Day=15 AND TimeDimension.Year=2009

Any hints welcome =)

like image 738
Aleksei Anufriev Avatar asked Oct 30 '09 13:10

Aleksei Anufriev


People also ask

What are included columns with SQL Server indexes?

Included columns can be varchar (max), nvarchar(max) , varbinary(max) or XML data types, that you cannot add it as index keys. Computed columns can also be used as included columns.

What is the difference between index key columns and included columns?

In the included columns the storage only happens on the leaf level of the index, contrary to the key column of an index. The key column of an index is stored at all levels. The non-key columns don't have the same limitations as the index key columns in terms of the data type.

Can we include columns in clustered index?

The reason for this is that when you apply a clustered columnstore index to a table, SQL Server changes the physical storage of the table to columns. As such, all columns are included in the clustered columnstore index.

What is an include clause in an index?

The Include Clause. The include clause allows us to make a distinction between columns we would like to have in the entire index (key columns) and columns we only need in the leaf nodes ( include columns). That means it allows us to remove columns from the non-leaf nodes if we don't need them there.


1 Answers

CREATE INDEX myindex ON mytablename (co1l, col2, col3, col4)

PostgreSQL does not support clustered or covering indexes.

Update:

For this query, you'll need to create the suggested index indeed:

SELECT  this_.id as id0_0_,   
        this_.device_id as device2_0_0_,  
        this_.time_id as time3_0_0_,  
        this_.gps_detail_id as gps4_0_0_   
FROM    DataMessage this_   
WHERE   this_.time_id = 65536
        AND this_.device_id = 32768

CREATE INDEX ix_datamessage_time_device_id_detail ON datamessage (time_id, device_id, id, gps_detail_id)

However, your tables seem to be over-normalized to me.

You can keep year, month and day in a single INT field in your table. This will save you a join.

There might be the point of keeping DataMessage and GpsDetails in separate tables if either GpsDetails are rarely linked to the DataMessage (this is, gps_details_id is often set to NULL), or a GPS details record can be shared between multiple data messages.

It it's not, it will be better to move the GPS details into the data messages table.

like image 189
Quassnoi Avatar answered Oct 27 '22 00:10

Quassnoi