Questions tagged [indexing]

Indexing data structures is a general technique to improve the speed of data lookups.

The purpose of storing an index is to optimize speed and performance in finding relevant documents for a search query. Without an index, the search process would scan every document in the corpus, which would require considerable time and computing power.

Indexes may benefit both read queries and updates. Many people wrongly believe indexes are only good for read queries. In general, there are three methods of indexing - non-clustered, clustered and cluster.

References:

Top Indexing Questions

By these questions, you will clear your concepts regarding indexing. And you can ask similar questions (not duplicate) with this tag.

33955 questions
527
votes
8 answers

Python Pandas: Get index of rows where column matches certain value

Given a DataFrame with a column "BoolCol", we want to find the indexes of the DataFrame in which the values for "BoolCol" == True I currently have the iterating way to do it, which works perfectly: for i in range(100,3000): if…
I want badges
  • 6,155
  • 5
  • 23
  • 38
524
votes
8 answers

Why use the INCLUDE clause when creating an index?

While studying for the 70-433 exam I noticed you can create a covering index in one of the following two ways. CREATE INDEX idx1 ON MyTable (Col1, Col2, Col3) -- OR -- CREATE INDEX idx1 ON MyTable (Col1) INCLUDE (Col2, Col3) The INCLUDE clause is…
Cory
  • 12,404
  • 7
  • 33
  • 28
515
votes
13 answers

What is an index in SQL?

Also, when is it appropriate to use one?
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219
513
votes
10 answers

Get first row value of a given column

This seems like a ridiculously easy question... but I'm not seeing the easy answer I was expecting. So, how do I get the value at an nth row of a given column in Pandas? (I am particularly interested in the first row, but would be interested in a…
Ahmed Haque
  • 7,174
  • 6
  • 26
  • 33
503
votes
8 answers

How do I add indexes to MySQL tables?

I've got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run SELECT * FROM table WHERE id = '1'; the code runs fine as the ID field is the primary index. However, for a recent development in the project, I have…
Michael
  • 5,505
  • 4
  • 20
  • 12
500
votes
10 answers

How to map/collect with index in Ruby?

What is the easiest way to convert [x1, x2, x3, ... , xN] to [[x1, 2], [x2, 3], [x3, 4], ... , [xN, N+1]]
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
470
votes
9 answers

Replace a character at a specific index in a string?

I'm trying to replace a character at a specific index in a string. What I'm doing is: String myName = "domanokz"; myName.charAt(4) = 'x'; This gives an error. Is there any method to do this?
kazinix
  • 28,987
  • 33
  • 107
  • 157
470
votes
9 answers

How do I handle too long index names in a Ruby on Rails ActiveRecord migration?

I am trying to add a unique index that gets created from the foreign keys of four associated tables: add_index :studies, ["user_id", "university_id", "subject_name_id", "subject_type_id"], :unique => true The database’s limitation for the index…
JJD
  • 50,076
  • 60
  • 203
  • 339
465
votes
13 answers

SQL join: selecting the last records in a one-to-many relationship

Suppose I have a table of customers and a table of purchases. Each purchase belongs to one customer. I want to get a list of all customers along with their last purchase in one SELECT statement. What is the best practice? Any advice on building…
netvope
  • 7,647
  • 7
  • 32
  • 42
441
votes
10 answers

How do MySQL indexes work?

I am really interested in how MySQL indexes work, more specifically, how can they return the data requested without scanning the entire table? It's off-topic, I know, but if there is someone who could explain this to me in detail, I would be very,…
good_evening
  • 21,085
  • 65
  • 193
  • 298
424
votes
4 answers

Is there an R function for finding the index of an element in a vector?

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively inefficient. Is there a more direct way to do it? For…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
410
votes
30 answers

List of all index & index columns in SQL Server DB

How do I get a list of all index & index columns in SQL Server 2005+? The closest I could get is: select s.name, t.name, i.name, c.name from sys.tables t inner join sys.schemas s on t.schema_id = s.schema_id inner join sys.indexes i on i.object_id =…
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
398
votes
9 answers

How can I index a MATLAB array returned by a function without first assigning it to a local variable?

For example, if I want to read the middle value from magic(5), I can do so like this: M = magic(5); value = M(3,3); to get value == 13. I'd like to be able to do something like one of these: value = magic(5)(3,3); value = (magic(5))(3,3); to…
Joe Kearney
  • 7,397
  • 6
  • 34
  • 45
392
votes
10 answers

Get column index from column name in python pandas

In R when you need to retrieve a column index based on the name of the column you could do idx <- which(names(my_data)==my_colum_name) Is there a way to do the same with pandas dataframes?
ak3nat0n
  • 6,060
  • 6
  • 36
  • 59
384
votes
6 answers

how do I insert a column at a specific column index in pandas?

Can I insert a column at a specific column index in pandas? import pandas as pd df = pd.DataFrame({'l':['a','b','c','d'], 'v':[1,2,1,2]}) df['n'] = 0 This will put column n as the last column of df, but isn't there a way to tell df to put n at the…
HappyPy
  • 9,839
  • 13
  • 46
  • 68