Skip to content

Instantly share code, notes, and snippets.

@emiliojva
Created April 16, 2021 06:15
Show Gist options
  • Save emiliojva/a35f1b3202fbcb35fb177fdc8111a255 to your computer and use it in GitHub Desktop.
Save emiliojva/a35f1b3202fbcb35fb177fdc8111a255 to your computer and use it in GitHub Desktop.
mysql-server-melhores-praticas-1

MySQL - Tunning

Aumentando a performance de retornos no MySQL Server.

BAIXAR E IMPORTAR, BANCO EXEMPLO 'SAKILA' EM : https://dev.mysql.com/doc/sakila/en/

Melhores práticas

Queries

  1. ELIMINAR LINHAS QUE SÃO DESNECESSÁRIAS - DISTINCT.
  2. ELIMINAR COLUNAS DESNECESSÁRIAS E EVITAR SEMPRE SELECT ALL(*) DAS COLUNAS
/**
	MELHORES PRATICAS
    
    BAIXAR E IMPORTAR, BANCO EXEMPLO 'SAKILA' EM : https://dev.mysql.com/doc/sakila/en/
	ELIMINAR LINHAS QUE SÃO DESNECESSÁRIAS - DISTINCT. 
	ELIMINAR COLUNAS DESNECESSÁRIAS E EVITAR SEMPRE SELECT ALL(*) DAS COLUNAS 
**/
use sakila;

/**BAD PRATICES**/
SELECT * FROM customer; -- todos os clientes 

/**TODOS OS ALUGUEIS POR CLIENTE **/
SELECT * FROM customer
INNER JOIN rental on rental.customer_id = customer.customer_id;

/** TODOS OS ALUGUEIS POR CLIENTE DE ACORDO COM A LOJA LOCATARIA **/
SELECT * FROM customer
INNER JOIN rental on rental.customer_id = customer.customer_id
INNER JOIN store on store.store_id = customer.store_id;

/** TODOS OS ALUGUEIS POR CLIENTE DE ACORDO COM A LOJA LOCATARIA. TRAZENDO DADOS DO VENDEDOR (STAFF)**/
SELECT * FROM customer
INNER JOIN rental on rental.customer_id = customer.customer_id
INNER JOIN store on store.store_id = customer.store_id
INNER JOIN staff on staff.staff_id = rental.staff_id;
/** OTIMIZAÇÃO 1 **/
SELECT 	customer.*,
		staff.first_name,
		staff.last_name
FROM customer
INNER JOIN rental on rental.customer_id = customer.customer_id
INNER JOIN store on store.store_id = customer.store_id
INNER JOIN staff on staff.staff_id = rental.staff_id;
/** OTIMIZAÇÃO 2 **/
SELECT 	DISTINCT -- TENTAR ELIMINAR LINHAS REPETIDAS
		customer.first_name,
		customer.last_name,
		staff.first_name,
		staff.last_name
FROM customer
INNER JOIN rental on rental.customer_id = customer.customer_id
INNER JOIN store on store.store_id = customer.store_id
INNER JOIN staff on staff.staff_id = rental.staff_id;

Written with StackEdit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment