Can you index elements of an array in Postgres? - arrays

I'm wondering if I can do something like
CREATE INDEX firstelement ON mytable (myarray[1]);
this particular syntax creates a syntax error.

Try this one, with extra parentesis:
CREATE INDEX firstelement ON mytable ((myarray[1]));

Related

How to use Postgresql GIN index with ARRAY keyword

I'd like to create GIN index on a scalar text column using an ARRAY[] expression like so:
CREATE TABLE mytab (
scalar_column TEXT
)
CREATE INDEX idx_gin ON mytab USING GIN(ARRAY[scalar_column]);
Postgres reports an error on ARRAY keyword.
I'll use this index later in a query like so:
SELECT * FROM mytab WHERE ARRAY[scalar_column] <# ARRAY['some', 'other', 'values'];
How do I create such an index?
You forgot to add an extra pair of parentheses that is necessary for syntactical reasons:
CREATE INDEX idx_gin ON mytab USING gin ((ARRAY[scalar_column]));
The index does not make a lot of sense. If you need to search for membership in a given array, use a regular B-tree index with = ANY.

Select Value of Parameter in Array with Denodo (VQL)

I am trying to do something that seems simple but cannot find the right syntax for Denodo's VQL (Virtual Query Language). I have a string like this: XXXX-YYYY-ZZZZ-AAAA-BBBB in a column called "location" that varies in length, and I want to get the value of the fourth set (i.e. AAAA in this example). I am using the Denodo split function like this:
SELECT SPLIT("-",location)[3] AS my_variable FROM my_table
However, the [3] doesn't work. I've tried a bunch of variations:
SELECT SPLIT("-",location)[3].value AS my_variable FROM my_table
SELECT SPLIT("-",location).column3 AS my_variable FROM my_table
etc.
Can someone please help me figure out the right syntax to return a single parameter from an array? Thank you!
SELECT field_1[3].string
FROM (SELECT split('-', 'XXXX-YYYY-ZZZZ-AAAA-BBBB') as field_1)
You have to do it using a subquery because the syntax to access the element of an array (that is, [<number>]) can only be used with field names. You cannot use something like [4] next to the result of a expression.
This question helps: https://community.denodo.com/answers/question/details?questionId=90670000000CcQPAA0
I got it working by creating a view that saves the array as a field:
CREATE OR REPLACE VIEW p_sample_data FOLDER = '/stack_overflow'
AS SELECT bv_sample_data.location AS location
, bv_sample_data.id AS id
, split('-', location) AS location_array
FROM bv_sample_data;
Notice I created a column called location_array?
Now you can use a select statement on top of your view to extract the information you want:
SELECT location, id, location_array[2].string
FROM p_sample_data
location_array[2] is the 3rd element, and the .string tells denodo you want the string value (I think that's what it does... you'd have to read more about Compound Values in the documentation: https://community.denodo.com/docs/html/browse/6.0/vdp/vql/advanced_characteristics/management_of_compound_values/management_of_compound_values )
Another way you could probably do it is by creating a view with the array, and then flattening the array, although I haven't tried that option.
Update: I tried creating a view that flattens the array, and then using an analytics (or "window") function to get a row_number() OVER (PARTITION BY id order by ID ASC), but analytic/window functions don't work against flat file sources.
So if you go the "flatten" route and your source system doesn't work with analytic fuctions, you could just go with a straight rownum() function, but you'd have to offset the value by column number you want, and then use remainder division to pull out the data you want.
Like this:
--My view with the array is called p_sample_data
CREATE OR REPLACE VIEW f_p_sample_data FOLDER = '/stack_overflow' AS
SELECT location AS location
, id AS id
, string AS string
, rownum(2) AS rownumber
FROM FLATTEN p_sample_data AS v ( v.location_array);
Now, with the rownum() function (and an offset of 2), I can use remainder division in my where clause to get the data I want:
SELECT location, id, string, rownumber
FROM f_p_sample_data
WHERE rownumber % 5 = 0
Frankly, I think the easier way is to just leave your location data in the array and extract out the nth column with the location_array[2].string syntax, where 2 is the nth column, zero based.

How do you iterate through the IndexCollection of each table in Microsoft.SqlServer.Management.Smo

I am trying to get lists of indexes and constraints per table in my Microsoft.SqlServer.Management.Smo application and I am having a hard time get the list of indexes to begin with.
If I am not mistaken each table has an Indexes property which is of type IndexCollection and it looks like it has a collection of IndexCollections as its items or elements.
For example if I have a reference to a SQL Server table called tbl I can refer to the indexes collection as follows:
tbl.Indexes
How do you iterate through the Indexes collection and get the list of indexes?
Try something like this:
// iterate over the indexes for this table
foreach (Index ix in tbl.Indexes)
{
string indexName = ix.Name;
string indexType = ix.IndexType.ToString();
string indexFilter = ix.FilterDefinition;
}
The IndexCollection is a collection of objects of type Index, which then contain a number of properties to get information about your indexes,.

Using INDEX in SQL Server

I need to create am index in SQL however it needs to display the records by entering only a part of the name. The index should be created to retrieve the student information using part of the student name (i.e. if the name if Johnanesburg; the user can input John)
I used the syntax below but it wont work
create index Student ON Student(SName)
SELECT * FROM Student WHERE StRegNo LIKE A%
go
I think your problem is here: A%
Try wrapping it in apostrophes.
SELECT *
FROM Student
WHERE StRegNo LIKE 'A%'
Also, you may want a GO statement after you create your index.
The index you are creating over SName will not provide as much benefit for the select statement you are running as one created over StRegNo. Assuming that StRegNo is the primary key on the Student table you could try:
CREATE CLUSTERED INDEX IX_Student on Student(StRegNo)
SELECT *
FROM Student
WHERE StRegNo LIKE 'A%'
However it appears that the SQL you have provided is at odds with your question. If you want to search based on student name then you might want the following instead.
CREATE NONCLUSTERED INDEX IX_Student on Student(SName)
SELECT *
FROM Student
WHERE SName LIKE 'A%'
Ardman got it right regarding your query %A => '%A'. Now as for the index, that's another story that no index can help you with at the time, neither can full text search. If you want to look for names starting with #A (i.e. John%), an ordered index could help but otherwise (i.e. %bur%), you will go for a full table scan !

Multi-variable indexes in postgres

Im looking at an application where I will be doing quite a few SELECTs where I am trying to find column_a = x AND column_b = y.
Is the correct to create that index that something like the following?
CREATE INDEX index_name ON table (column_a, column_b)
Yes.

Resources