SQL way to index across tables?

create index for join table
sql index
sqlite index multiple tables
show index from multiple tables
multiple indexes
create index on join columns
sql join index
index table

Is there a way to create a multi column index across tables?

For example, if I had the following tables:

Foo (Table Name)
  FooID (PK)
  BarID (FK)
  FooName

Bar (Table Name)
  BarID (PK)
  BarName

I can do a

SELECT * 
FROM Foo 
LEFT JOIN Bar ON Foo.BarID = Bar.BarID
WHERE 
  FooName < "Bob"
  AND BarName > "Smith";

In this case, I want a multi column index against Foo.FooName then Bar.BarName.

I did some research but wasn't able to find anything, perhaps I'm not using the right terms. My question may depend on the SQL engine, in which case I'm interested in MySQL specifically, but I am interested in any other engines as well.

Doing the multi column index on Foo with the Foreign Key doesn't help, as the underlying value of its Name is what I want for the speed.

Came across my own post years later and figured I could add some details, in case others have similar issues. As Mark B pointed out, each index is per table, however we can set things up that make this efficent, see below.

There are a couple of different things going on here, so can use indexes to help accomplish what we need. We need an index to help filter the main table, then an index that works well for the join and filter of the 2nd table. To help accomplish this, we can create the following 2 indexes:

CREATE INDEX idx_fooname ON Foo (FooName);
CREATE INDEX idx_barid_barname ON Bar (BarID, BarName);

Once those indexes are in place, a query can be used like:

SELECT * 
FROM Foo USE INDEX(idx_fooname)
LEFT JOIN Bar USE INDEX (idx_barid_barname) ON Foo.BarID = Bar.BarID
WHERE 
  FooName < "Bob"
  AND BarName > "Smith";

How to Index a JOIN result of two or more tables in order to improve , A foreign key does not have an index by default. You have to create one. In this case, I'd suggest either of these. It depends on relative selectivity of the 2  Is there a way to create a unique index across tables in a MS SQL database? I have two tables, Table A and Table B. In that I have a Column named ID, I want to make these Id columns as unique (combining Table A and B's Ids )

Smells like "over-normalization". Might it be worth moving those two fields into the same table?

Akiban was an Engine that could do cross-table JOINs, etc. But it no longer exists.

"Materialized Views" do not exist in MySQL (unless you implement them yourselves).

Index merge: using multiple indexes for one table access, The answer is very simple in most cases: one index with multiple columns is do a perfect job, no matter how you define the index; e.g., queries with two or more I make my living from training, other SQL related services and selling my book. SQL CREATE INDEX Statement. The CREATE INDEX statement is used to create indexes in tables. Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.

As xQbert mentioned, you can use materialized views (or indexed views if you use Microsft SQL Server).

But first you have to change your LEFT JOIN to a INNER JOIN (because of materialized/indexed views can't handle outer joins. The left join does not seem to make sense in your query because of the WHERE ... BarName > "Smith".

That link might help you if you use SQL Server: https://www.simple-talk.com/sql/learn-sql-server/sql-server-indexed-views-the-basics/

After you created a materialized/indexed view, you can query the view directly (i'm not sure if the query optimizer will use it automatically). Be aware that the materialized/indexed view will reduce your performance when you INSERT, DELETE or UPDATE into the used tables (what every index will do, too). The best idear is only to add the realy neccesary fields to the materialized/indexed view.

Single Index on multiple tables? – SQLServerCentral, So that's 4 T-SQL objects for one business requirement, and its causing maintenance issues. Is there an easier way? - perhaps something to do  In SQL Server CREATE INDEX command creates a relational index on a table or view. Also called a rowstore index because it is either a clustered or nonclustered btree index. You can create a rowstore index before there is data in the table.

How to create a Unique (Index) constraint across multiple tables , Is there a way to create a unique index across tables in a MS SQL database? I have two tables, Table A and Table B. In th | 6 replies  The easiest way to create an index is to go to Object Explorer, locate the table, right-click on it, go to the New index, and then click the Non-Clustered index command: This will open the New index window in which we can click the Add button on the lower right to add a column to this index:

An Essential Guide to SQL Server Indexes, SQL Server provides two types of indexes: clustered index and non-clustered index. Disable indexes – show you how to disable indexes of a table to make the  SQL Server supports table and index partitioning. The data of partitioned tables and indexes is divided into units that can be spread across more than one filegroup in a database. The data is partitioned horizontally, so that groups of rows are mapped into individual partitions.

SQL CREATE INDEX Statement, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  I've read about indexing the foreign key in order to improve the performance of a JOIN result. Do i need to add the foreign key column to an additional non-clustered index or the foreign key has a default indexing? How can i effectively implement indexing if my SQL table structure is as follows and i've a JOIN query with WHERE clause using t1_col3

Comments
  • you can't. indexes are per-table. just put separate individual indexes on the two fields and move onwards.
  • You could if you create a materialized view however; and used it for queries. but @MarkB is right, you can't do it across tables without other intervention. But Materialized Views have other considerations and add to complexity of solution.
  • Note that your example query is an implicit INNER JOIN because you have AND BarName > "Smith" in the WHERE clause. This should be AND (BarName > "Smith" OR BarName IS NULL) to retain the LEFT JOIN. Alternately, you can put the AND BarName > "Smith" in the ON clause and filter before the JOIN.
  • Looks like MySQL has view ability too, however one of the restrictions is no indexing on views... dev.mysql.com/doc/refman/5.0/en/view-restrictions.html :(