Entity Framework 6.2 index with include columns and desc sort column within the index - entity-framework-6

I'm trying to create an index in Entity Framework 6.2 using code first or fluent API.
This is the SQL version of the index that I want to create
CREATE NONCLUSTERED INDEX [IX_My_Index] ON [dbo].[Table1]
(
[RecordId] ASC,
[RecordType] ASC,
[RecordDate] DESC,
[Id] ASC
)
INCLUDE([Column1],[Column2],[Column3])
My 2 problems are the "Include" columns and the RecordDate desc within the index.
Is this possible using code first attributes or Fluent API?

Related

Solving a PG::GroupingError: ERROR

The following code gets all the residences which have all the amenities which are listed in id_list. It works with out a problem with SQLite but raises an error with PostgreSQL:
id_list = [48, 49]
Residence.joins(:listed_amenities).
where(listed_amenities: {amenity_id: id_list}).
references(:listed_amenities).
group(:residence_id).
having("count(*) = ?", id_list.size)
The error on the PostgreSQL version:
What do I have to change to make it work with PostgreSQL?
A few things:
references should only be used with includes; it tells ActiveRecord to perform a join, so it's redundant when using an explicit joins.
You need to fully qualify the argument to group, i.e. group('residences.id').
For example,
id_list = [48, 49]
Residence.joins(:listed_amenities).
where(listed_amenities: { amenity_id: id_list }).
group('residences.id').
having('COUNT(*) = ?", id_list.size)
The query the Ruby (?) code is expanded to is selecting all fields from the residences table:
SELECT "residences".*
FROM "residences"
INNER JOIN "listed_amenities"
ON "listed_amentities"."residence_id" = "residences"."id"
WHERE "listed_amenities"."amenity_id" IN (48,49)
GROUP BY "residence_id"
HAVING count(*) = 2
ORDER BY "residences"."id" ASC
LIMIT 1;
From the Postgres manual, When GROUP BY is present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions or if the ungrouped column is functionally dependent on the grouped columns, since there would otherwise be more than one possible value to return for an ungrouped column.
You'll need to either group by all fields that aggregate functions aren't applied to, or do this differently. From the query, it looks like you only need to scan the amentities table to get the residence ID you're looking for:
SELECT "residence_id"
FROM "listed_amenities"
WHERE "listed_amenities"."amenity_id" IN (48,49)
GROUP BY "residence_id"
HAVING count(*) = 2
ORDER BY "residences"."id" ASC
LIMIT 1
And then fetch your residence data with that ID. Or, in one query:
SELECT "residences".*
FROM "residences"
WHERE "id" IN (SELECT "residence_id"
FROM "listed_amenities"
WHERE "listed_amenities"."amenity_id" IN (48,49)
GROUP BY "residence_id"
HAVING count(*) = 2
ORDER BY "residences"."id" ASC
LIMIT 1
);

Order by foreign key in activerecord: without a join?

I want to expand this question.
order by foreign key in activerecord
I'm trying to order a set of records based on a value in a really large table.
When I use join, it brings all the "other" records data into the objects.. As join should..
#table users 30+ columns
#table bids 5 columns
record = Bid.find(:all,:joins=>:users, :order=>'users.ranking DESC' ).first
Now record holds 35 fields..
Is there a way to do this without the join?
Here's my thinking..
With the join I get this query
SELECT * FROM "bids"
left join users on runner_id = users.id
ORDER BY ranking LIMIT 1
Now I can add a select to the code so I don't get the full user table, but putting a select in a scope is dangerous IMHO.
When I write sql by hand.
SELECT * FROM bids
order by (select users.ranking from users where users.id = runner_id) DESC
limit 1
I believe this is a faster query, based on the "explain" it seems simpler.
More important than speed though is that the second method doesn't have the 30 extra fields.
If I build in a custom select inside the scope, it could explode other searches on the object if they too have custom selects (there can be only one)
What you would like to achieve in active record writing is something along
SELECT b.* from bids b inner join users u on u.id=b.user_id order by u.ranking desc
In active record i would write such as:
Bids.joins("inner join users u on bids.user_id=u.id").order("u.ranking desc")
I think it's the only to make a join without fetching all attributes from the user models.

Can I add a compound index that includes the table's primary key when using Core Data with a SQLite backing store?

I am working on an iOS 7 app that is using Core Data persisted with a SQLite store. I am trying to optimize for reading.
I've been following the advice in this blog post, which suggests using compound indices that will include columns used in both the where clause and the order by clause of slow queries. However, one of my queries is selecting with a where clause based on the Core Data managed primary key being in a subquery:
SELECT 0, t0.Z_PK, t0.Z_OPT, ... FROM ZMODEL t0
WHERE t0.Z_PK IN (SELECT * FROM _Z_intarray0)
ORDER BY t0.ZTITLE LIMIT 99999
I can't get SQLite to explain the query for me exactly since _Z_intarray0 is a temporary table (or is it?), but I can substitute some values in to (potentially) get some guidance from the database:
explain query plan SELECT 0, t0.Z_PK, t0.Z_OPT, ... FROM ZMODEL t0
WHERE t0.Z_PK IN (1,2,3)
ORDER BY t0.ZTITLE LIMIT 99999;
0|0|0|SEARCH TABLE ZMODEL AS t0 USING INTEGER PRIMARY KEY (rowid=?) (~3 rows)
0|0|0|EXECUTE LIST SUBQUERY 1
0|0|0|USE TEMP B-TREE FOR ORDER BY
I would think that at the database level I could add an index on the primary key and the title column in order to not generate a temporary index for the order by clause; however, Core Data is managing the primary key and I cannot add a multicolumn index with it since it is not an object-level attribute.
Is there a way to speed up this query with an index or another solution?

Lucene Grails Searchable plugin build index for part of the table

I have a searchable domain class mapped to a table that has a flag column. Currently, when Lucene creates an index it generates a query like this (returns all data in the table):
select this_.id as id0_0_,
this_.flag as flag2_0_0_,
this_.email as email0_0_, this_.first_name as first6_0_0_, this_.last_name as last8_0_0_
from ais_person this_
order by this_.id asc
Is it possible to build an index for only those rows which contain a specific flag value, so that the generated query would look like this:
select this_.id as id0_0_,
this_.flag as flag2_0_0_,
this_.email as email0_0_, this_.first_name as first6_0_0_, this_.last_name as last8_0_0_
from ais_person this_
where this_.flag = 'Y'
order by this_.id asc
Yes, i think you can do that. Following is the pseudo-code.
Document doc = new Document();
doc.add(Field.Text("flag", Y));

Can the sortable_table plugin do order by multiple columns?

I'm using the sortable_table plugin for sorting an HTML table in a Rails app.
If we want to order by multiple columns, can sortable_table plugin do it
?
Example:
select distinct brands.title as brands, models.title as model
from brands, models
where
brands.id = models.brand_id
order by brands.title desc, models.title desc
Oh.. I get it!
Just put sortable_attributes on controller as array.
For example:
sortable_attributes :brand_model => ["brands.title", "models.title"]

Resources