How could a SELECT perform better with multiple INNER JOIN

Asked

Viewed 35 times

0

I have a SELECT with several INNER JOIN and some functions such as replace, if..

That one SELECT takes all the information I need, but it takes too long.

Is there any way to optimize this SELECT?

SELECT
as_assinaturas.id, 
as

_clientes.id, 
as_clientes.nome, 
as_clientes.email, 

CONCAT(as_enderecos.endereco, as_enderecos.complemento), 

as_enderecos.bairro,
as_enderecos.cidade, 
as_estados.sigla, 
as_enderecos.cep, "Brasil" AS "País", 

as_clientes.telefone, 
as_assinaturas.ed_inicial,
(as_campanhas.edicoes + as_assinaturas.ed_inicial + as_assinaturas.ed_ajuste_imp) - 1,
as_produtos.titulo, 
as_campanhas.edicoes,
as_tipo_assinaturas.nome, 

REPLACE(REPLACE(REPLACE(REPLACE(as_assinaturas.status, 'I', 'Inativo'), 'A', 'Ativo'), 'C', 'Cancelado'), 'B', 'Bloqueado') AS "STATUS", 
as_assinaturas.status,
as_assinaturas.created,
as_pagamentos.valor,
as_pagamentos.data_pagamento, 
as_ordens.tipo_de_pagamento, 
IF(as_campanhas.nome LIKE('%Migra%'),'content','digisa') AS "OBSERVACAO", 
as_ordens.contrato,
as_assinaturas.vendedor, 
as_campanhas.distribuicao, 
as_assinaturas.id AS "ID LEGADO", 
as_representantes.nome 

FROM as_clientes 

INNER JOIN as_assinaturas ON as_clientes.id = as_assinaturas.cliente_id 
INNER JOIN as_enderecos ON as_enderecos.cliente_id = as_clientes.id 
INNER JOIN as_estados ON as_enderecos.estado_id = as_estados.id
INNER JOIN as_campanhas ON as_assinaturas.campanha_id = as_campanhas.id 
INNER JOIN as_produtos ON as_produtos.id = as_campanhas.produto_id 
INNER JOIN as_pagamentos ON as_assinaturas.id = as_pagamentos.id 
INNER JOIN as_ordens ON as_assinaturas.id = as_ordens.assinatura_id
INNER JOIN as_tipo_assinaturas ON as_tipo_assinaturas.id = as_assinaturas.tipo_assinatura_id
INNER JOIN as_representantes ON as_representantes.id = as_assinaturas.representante_id

WHERE as_assinaturas.status = "A"

LIMIT 3
  • 4

    You need to first understand where the problem is by using some tool that can generate the execution plan (query plan) of your query. There may be several causes: some id not indexed, too much information returned (you did not inform us how many records are returned), how many records exist in each table, if the status is indexed, etc. There are many variables, not knowing the tables, indexes and amount of records, number of records returned, etc... it is difficult to help.

  • it is certain that these Places and the like are getting in the way, but without a minimal example we will not be able to do anything. Try to mount the environment in Sqlfiddle

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.