Do a join make the search faster?

3

I have two tables, one for patients and one for queries. In summary, the tables have the following structure:

Patients

int id;
string nome;
string cartaoSus;

Queries

int id;
int idPaciente;
dateTime data;

When I want a patient report with scheduled appointments, I do so:

//Crio uma classe que engloba as duas tabelas:
private class consultaCompleta
{
    int idConsulta;
    int idPaciente;
    dateTime data;
    string nome;
    string cartaoSus;
}

//Crio as listas referentes à cada uma das tabelas:
private void montaConsulta(int idConsulta)
{
    List<pacientes> listaPaciente = model.pacientes.ToList(); //pego tudo da tabela
    List<consultas> listaConsulta = model.consultas.ToList(); //pego tudo da tabela
    List<consultaCompleta> listaConsultaCompleta = (from c in listaConsulta
                                                    join p in listaPaciente on c.idPaciente equals p.id
                                                    select new consultaCompleta()
                                                    {
                                                        idConsulta = c.id,
                                                        idPaciente = p.id,
                                                        data = c.data,
                                                        nome = p.nome,
                                                        cartaoSus = p.cartaoSus
                                                    })
                                                    .Where(p => p.id.Equals(idConsulta)) 
                                                    .ToList();
}

The biggest question is this: I always get all the data from the tables and join later. Is there another way to do this by grabbing the table first (how did bigown respond to me)? How to do it then?

I'm sorry to have practically redone the whole question, but I need to know this issue of slow queries, as my bank will look great in the future.

    
asked by anonymous 05.07.2017 / 15:11

1 answer

3

It will probably get faster, the database will be able to optimize, and bring less information. Just testing to find out. But you have to do the right tests. But whenever you pick up various information and try to filter on the client side it tends to be slower.

I believe it will bring more consistent results. Between picking up information from one table and then picking up from something else can change and become inconsistent. Catching everything at once in the same query ensures consistency. I just can not guarantee why I do not know if everything is generated in the same transaction, and how is the database configuration.

It would only tell you how to do it if you had information in the question about the template, how are you using it.

To finish, I understand what you mean, but probably the term lambda is being misused . See what a lambda .

    
05.07.2017 / 15:17