TheJediCowboy
TheJediCowboy

Reputation: 9222

Best SQL Option for Performance

I currently need to execute a database query that involves data from three separate tables. (bases,member,sell_transaction)

The bases table looks like the following

base_id(PK)    name    state

The member table looks like the following

id(PK)    last_name    username   email    first_name

The sell_transaction table has the following schema

transaction_id(PK)    city     state     base_id     date    id    agentId

NOTE- these tables contain more columns, but they are irrelevant, so I am going to save the time and not write them out.

I am working on a transaction inquiry that involves data from all of these tables. I need to return this data in a TransactionItem that I am populating from these three tables

The TransactionItem as a Pojo looks like the following

public TransactionItem(){

    private String firstName;
    private String lastName;
    private String email;
    private String baseName;
    private String city;
    private String state;
    private Date date;

    public String getFirstName(){
              return firstName;
    }
    public String setFirstName(String firstName){
              this.firstName = firstName;
    }
    ...
    ...///The rest of the getters and setters
    ...
    ...


}



Currently I am doing three separate selects from each table, which is taking longer then I would like. I would image that there is a way I could use a join or nested select statements,etc.

The data and conditions and their corresponding tables are as follows

I need...

first_name, last_name,id from member

I need

name and state from bases where base_id from sell_transaction is equal to base_id from bases

Again there is more that I need, but it is redundant to name them off, The idea should still be clear, I need to query data that is dependent on each other. I am not sure performance wise what is the best performance option. The amount of data that is being traversed could end up being very large or very small(not sure if that matters).

select b.name,b.state, s.city,s.state,s.duty,s.branch,s.date, m.first_name,m.last_name,m.email,m.phone,m.id,s.transaction_id from bases as b, sell_transaction as s, member as m where s.agentId is null and s.username = m.username and b.base_id = s.duty

Upvotes: 1

Views: 165

Answers (1)

select first_name, last_name, member.id member_id, bases.name, bases.state
from bases, sell_transaction, member
where sell_transaction.base_id = bases.base_id
and member.id = sell_transaction.agentId;

I am going out on a limb here and guessing that agentID is the connection to member from sell_transaction, but it's not clear or obvious, and possibly incorrect (unless you clarify your data model).

Upvotes: 1

Related Questions