I have a slow query I'm trying to speed up:
#Query("select max(t.timestamp) from TradeDbo t where t.currencyPair.id = :currencyPairId")
Date findMaxTimestamp(#Param("currencyPairId") Long currencyPairId);
The entity is defined by:
#Entity()
#Table(name = "Trade",
indexes = { #Index(name = "idx_timestamp_currencypairid",
columnList = "timestamp,currency_pair_id")})
public class TradeDbo extends Auditable<String> {
#Id #GeneratedValue
#Getter private Long id;
#Version
private long version;
#JoinColumn(name = "currency_pair_id")
#ManyToOne(fetch=FetchType.EAGER)
#Getter private CurrencyPairDbo currencyPair;
#Column(name = "timestamp")
#Convert(converter = DateConverter.class)
#Getter private Date timestamp;
...
and, as you can see, I've defined an index on the timestamp/currencypairid, (see how to speed up max() query) which I thought would have made max(timestamp) just a read of the last page of the btree, but it's still taking as long as it did before adding the index.
do your query as
"select max(t.timestamp) from TradeDbo t where t.currencyPair_id = :currencyPairId"
Your composite index is not usefult for you performance you should change the column sequence
columnList = "currency_pair_id,timestamp"
using first the column involved in where condition ..
the index content build using the columns is used reading for left to right
Related
I would like to set indexes on multiple columns within a single table in MySQL database. After reading this article, I'm not 100% sure which approach to use.
So my (simplified) table looks like this:
#Data
#Entity
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "loan")
public class Loan {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "loan_id", unique = true, nullable = false)
private long id;
#Column(name = "amount", unique = false, nullable = false)
private double amount;
#Column(name = "rate", unique = false, nullable = false)
private double rate;
#Column(name = "payments", unique = false, nullable = false)
private int payments;
#Column(name = "pmt", unique = false, nullable = false)
private double pmt;
}
I will have a lot of search queries, for instance:
SELECT *
FROM Loan loan
WHERE loan.amount =: amount
AND loan.rate =: rate
AND loan.payments =: payments
AND loan.pmt =: pmt
LIMIT 1;
Now, I would like to index fields in WHERE clause. Essentially, I would like to achieve effect of a "composite key" where in table loan there are only unique combinations of mentioned fields. So I cannot have two rows all with some values.
Is there such a configuration?
ou can add a UNIQUE constraint, which would be indexed automatocally
#Table(uniqueConstraints =
{
#UniqueConstraint(name = "UniqueWhereclause", columnNames = { "amount", "rate","payments","pmt" })})
or you can create an index alone
#Entity
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "loan", indexes = {
#Index(columnList = "amount, rate,payments,pmt", name = "name_idx") })
DOUBLE is likely to cause trouble for you. Switch to DECIMAL with a suitable number of decimal places -- so that = can be tested correctly.
Also, that's a strange combination of 4 things to use in the WHERE. Any 3 of those columns should (mathematically) determine the value for the 4th.
Does the table also have the name of the person taking out the loan? Or is this table a list of possible loans? As rates change, so you add more rows to the table? But why have the table if 3 columns can be used to compute the 4th?
How can I create a Single Entity from two other tables/Entity in JPA/hibernate making a INNER JOIN result with a common key. I was using the below code but it gives me a full join instead of an inner join. it give me records from the meal table even if the
"id 1" does not exist in the allergies table, example:
{id=1, name='tacos', description='Mexican food', price ='10',peanuts=null, celery=null, sesameSeeds=null}
How can constrain to don't return any records if the 'id' is missing from the secondary table allergies? to show only records when the primary key is present in both tables.
I want something like this instead:
{id=1, name='tacos', description='Mexican food', price ='10',peanuts='no', celery='no', sesameSeeds='no'}
Please advise.
#Entity
#Table(name = "meal")
#SecondaryTable(name = "allergens", pkJoinColumns = #PrimaryKeyJoinColumn(name = "meal_id"))
class Meal {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
Long id;
#Column(name = "name")
String name;
#Column(name = "description")
String description;
#Column(name = "price")
BigDecimal price;
#Column(name = "peanuts", table = "allergens")
boolean peanuts;
#Column(name = "celery", table = "allergens")
boolean celery;
#Column(name = "sesame_seeds", table = "allergens")
boolean sesameSeeds;
// standard getters and setters
}
I use hibernate and MySQL.
I have a query in MySQL database which looks like:
select
phones0_.c_id as c_id_1, phones0_.cp_id as cp_id1_2, phones0_.cp_phone as cp_phon1
from
customer_phone phones0
where
phones0_.c_id in (select customer0_.c_id from customer customer0_ )
The issue is that there is no where clause in nested select. But I always use restrictions when i do some operations in JpaRepository.
I guess there are some issues in mapping.
Here are the examples of entities:
#Data
#Entity
class Customer {
#GeneratedValue
#Id
#Column(name="c_id")
Long id;
#Column(name="c_name")
private String name;
#OneToMany( mappedBy = "customer" )
#Fetch( FetchMode.SUBSELECT )
private List<CustomerPhone> phones;
}
#Data
#Entity
#Table(name = "customer_phone")
class CustomerPhone {
#GeneratedValue
#Id
#Column(name="cp_id")
Long id;
#Column(name="cp_phone")
private String phone;
#ManyToOne( fetch = FetchType.LAZY )
#JoinColumn( name = "id", nullable = false, updatable = false )
private Customer customer;
}
I know the problem with N+1 select is well-known, but trying using different fetching strategies does not help to avoid it when I use native query.
I have 2 entities: A and B that correspond to the mysql tables A and B.
Several rows in table A could have the same b_id, that's why I use #ManyToOne annotation.
#Entity
#Table(name = "A")
public class A {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
#ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#Fetch(FetchMode.JOIN)
private B b;
}
I also create entity B.
#Entity
#Table(name = "B")
public class B {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String url;
}
I need to find all rows in A that are linked only for 1 row in table B. So I create native query in my ARepository
public interface ARepository extends JpaRepository<A, Integer> {
#Query(value = "SELECT a.id, a.b_id, a.name, b.url "
+ "FROM a "
+ "JOIN b ON a.b_id = b.id "
+ "GROUP BY a.b_id "
+ "HAVING count(a.b_id) = 1, nativeQuery = true)
List<A> getLinkedOnce();
But when when I run my restcontroller that just call getLinkedOnce method from repository, I see that in console, 1-st my query is called, then there are number of selects for each row in A that are end with
from B b0_ where b0_.id=?
I try to use different approaches, LAZY, EAGER and it does not work. I think because there is a usage of native query. But maybe the reason is another.
My object model is given below and would like your inputs on the number of indexes to create for faster query responses (on h2, mysql). Assumptions and questions are given below the following model.
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
private Integer id;
#ManyToOne(fetch = FetchType.LAZY)
#ForeignKey(name = "fk_user_org_id")
#Index(name = "idx_user_org_id")
#JoinColumn(name = "org_id", nullable = false, referencedColumnName = "id")
#NotNull
private Organization organization;
#ManyToOne(fetch = FetchType.LAZY)
#ForeignKey(name = "fk_user_year_id")
#Index(name = "idx_user_year_id")
#JoinColumn(name = "year", nullable = false, referencedColumnName = "id")
#NotNull
private Year year;
#ManyToOne(fetch = FetchType.LAZY)
#ForeignKey(name = "fk_user_created_by")
#Index(name = "idx_user_created_by")
#JoinColumn(name = "created_by", nullable = false, referencedColumnName = "id")
#NotNull
private User createdBy;
#Column(name = "name", nullable = false)
private String name;
#Column(name = "desc")
private String desc;
#Column(name = "is_system", length = LEN_1)
#Type(type = "org.hibernate.type.YesNoType")
private boolean isSystem = false;
#Column(name = "user_type", nullable = false)
private UserType userType;
#Column(name = "status", nullable = false)
#NotNull
private Status status;
}
Our plan is to use multi column indexes instead of a single column index (i.e. create index user_idx based on (organization, year, isSystem, status, userType, createdBy)). Assuming I have this index, will I get optimized responses for my queries listed below.
select * from user where organization=1 and year=2010;
select * from user where organization=1 and year=2010 and isSytem=true or false; (i.e. system users or application defined users)
select * from user where organization=1 and year=2010 and isSytem=false and userType=Manager (i.e. all managers)
select * from user where organization=1 and year=2010 and isSytem=false and userType=Employee (i.e. all employees)
select * from user where organization=1 and year=2010 and isSytem=false and userType=Manager and status=ACTIVE (i.e. Active users)
select * from user where organization=1 and year=2010 and createdBy='Sam' or 'Joe'
Does [6] need a different multi column index, consisting of the above 3 columns?
Since we are creating a multi column index as per my original assumption, can I safely remove the individual indexes (idx_user_org_id, idx_user_year_id, idx_user_created_by) as currently defined in the model?
You should switch the order of the columns in your index:
(organization, year, isSystem, userType, status, createdBy)
This allows it to better serve these two queries:
select * from user where organization=1 and year=2010 and isSystem=false and userType=Manager
select * from user where organization=1 and year=2010 and isSystem=false and userType=Employee
Does [6] need a different multi column index, consisting of the above 3 columns?
It doesn't need a new index - it can use the existing one but in a less efficient way - only the first two columns will be used. Adding a new index for this query looks like a good idea though.
can I safely remove the individual indexes
Yes. You should remove unused indexes otherwise they will just take up disk space and slow down table modifications without providing any benefit.