sql query speed. Time response to long - mysql

I have problem with my mysl query. Response time is to long. My query:
SELECT SQL_CALC_FOUND_ROWS
t.id,
l.id_produkt_lista,
z.nr_zam,
k.nazwa_fv,
p.nazwa,
p.opis,
p.data_realizacji,
CONCAT(t.d_graf,'</br>', IF(t.D_d_graf IS NOT NULL,
DATE_FORMAT(t.D_d_graf, "%d-%m-%Y"),"")),
CONCAT(t.d_druk,'</br>', IF(t.D_d_druk IS NOT NULL,
DATE_FORMAT(t.D_d_druk, "%d-%m-%Y"),"")),
CONCAT(t.d_zgrz,'</br>', IF(t.D_d_zgrz IS NOT NULL,
DATE_FORMAT(t.D_d_zgrz, "%d-%m-%Y"),""))
FROM zamowienie_produkt_lista l
JOIN zamowienia_zamowienie z ON (l.id_zamowienie = z.id_zamowienie)
JOIN zamowienia_produkt p ON (l.id_produkt = p.id_produkt)
JOIN zamowienia_prac_tmp t ON (l.id_produkt = t.id_produkt)
JOIN zamowienia_klient k ON (z.id_klient = k.id_klient)
WHERE TRUE
AND id_typ_produkt = '1'
AND z.archiwum = 0
ORDER BY t.id
When I use it in phpmyadmin I have to wait about 10 second

Assuming that id_typ_produkt belongs to table zamowienie_produkt_lista, creating following indexes should help:
CREATE INDEX p_1_idx ON zamowienie_produkt_lista
(id_produkt, id_typ_produkt);
CREATE INDEX z_1_idx ON zamowienia_zamowienie
(id_zamowienie, archiwum);
You should also make sure that indexes on all *_id fields for all other joined tables exist.

Related

Optimization of a large database query

I would like to ask if there is any way to optimize the following query:
SELECT SQL_BIG_RESULT DISTINCT u.spy_tech,
u.computer_tech,
u.military_tech,
u.defence_tech,
u.shield_tech,
u.energy_tech,
u.hyperspace_tech,
u.combustion_tech,
u.impulse_motor_tech,
u.hyperspace_motor_tech,
u.laser_tech,
u.ionic_tech,
u.buster_tech,
u.intergalactic_tech,
u.expedition_tech,
u.metal_proc_tech,
u.crystal_proc_tech,
u.deuterium_proc_tech,
u.technologia_bankowa,
u.technologia_kolonizacyjna,
u.technologia_optymalizacji,
u.technologia_otchlani,
u.graviton_tech,
SUM(p.small_ship_cargo) as small_ship_cargo,
SUM(p.big_ship_cargo) as big_ship_cargo,
SUM(p.light_hunter) as light_hunter,
SUM(p.heavy_hunter) as heavy_hunter,
SUM(p.crusher) as crusher,
SUM(p.battle_ship) as battle_ship,
SUM(p.colonizer) as colonizer,
SUM(p.recycler) as recycler,
SUM(p.spy_sonde) as spy_sonde,
SUM(p.bomber_ship) as bomber_ship,
SUM(p.solar_satelit) as solar_satelit,
SUM(p.destructor) as destructor,
SUM(p.dearth_star) as dearth_star,
SUM(p.battleship) as battleship,
SUM(p.lune_noir) as lune_noir,
SUM(p.ev_transporter) as ev_transporter,
SUM(p.star_crasher) as star_crasher,
SUM(p.giga_recykler) as giga_recykler,
SUM(p.dm_ship) as dm_ship,
SUM(p.apocalypse) as apocalypse,
SUM(p.annihilator) as annihilator,
SUM(p.devastator) as devastator,
SUM(p.fleet_save) as fleet_save,
u.r_badawczy,
u.r_budowlany,
u.r_militarny,
u.r_obrony,
u.r_paliwowy,
u.r_gospodarczy,
u.r_planetarny,
u.r_magazynowy,
u.r_energetyczny,
u.r_ekonomiczny,
u.r_odkrywczy,
u.r_max,
u.r_speed,
u.r_moon,
u.r_dm,
u.r_terra,
SUM(p.misil_launcher) as misil_launcher,
SUM(p.small_laser) as small_laser,
SUM(p.big_laser) as big_laser,
SUM(p.gauss_canyon) as gauss_canyon,
SUM(p.ionic_canyon) as ionic_canyon,
SUM(p.buster_canyon) as buster_canyon,
SUM(p.small_protection_shield) as small_protection_shield,
SUM(p.big_protection_shield) as big_protection_shield,
SUM(p.planet_protector) as planet_protector,
SUM(p.graviton_canyon) as graviton_canyon,
SUM(p.orbital_station) as orbital_station,
SUM(p.dzialo_pulsacyjne) as dzialo_pulsacyjne,
SUM(p.dzialo_fotonowe) as dzialo_fotonowe,
SUM(p.interceptor_misil) as interceptor_misil,
SUM(p.interplanetary_misil) as interplanetary_misil,
u.id,
u.ally_id,
u.onlinetime,
s.tech_rank AS old_tech_rank,
s.build_rank AS old_build_rank,
s.defs_rank AS old_defs_rank,
s.fleet_rank AS old_fleet_rank,
s.total_rank AS old_total_rank,
s.total_points_old AS total_points_old,
s.fleet_points_old AS fleet_points_old,
s.defs_points_old AS defs_points_old,
s.build_points_old AS build_points_old,
s.tech_points_old AS tech_points_old
FROM test_users as u
LEFT JOIN test_statpoints as s ON s.stat_type = 1 AND s.id_owner = u.id
LEFT JOIN test_planets as p ON u.id = p.id_owner
WHERE u.user_lastip != "127.0.0.1"
AND u.bana = "0"
AND u.authlevel = "0"
AND u.universe = "1"
GROUP BY s.id_owner, u.id
On my VPS, the query takes 8 seconds and the result is about 10,000 results. Although I improved the machine twice, to 8x2,4CPU, 16gb RAM, the query time has not changed.
So I thought to optimize the above-mentioned query or work on VPS configuration and find a bottleneck that can generate such time.
Table STATPOINTS:
Table USERS:
I do not know if I have given you an insight into the tables, when it comes to something else I would ask for guidance.
Explain:
Your query can run faster if you add the following indexes:
create index ix1 on user (bana, authlevel, universe);
create index ix2 on test_statpoints (stat_type, id_owner);
create index ix3 on test_planets (id_owner);
They include the access columns according to the join predicates.

Query blocking entirely MySQL server

I try to a run a specific query. However, when I execute it, the MySQL server doesn't respond anymore.
There is approximately 30000 rows in the table base_contrats_actifs but I don't know if this is a problem.
Here is the query :
UPDATE
base_contrats_actifs a
SET
a.code_indice = (
SELECT
MAX(g.code_indice)
FROM
base_gid g
WHERE
a.num_version = g.num_version_contrat
),
a.flag_bailleur_locataire = (
SELECT
MAX(g.flag_bailleur_locataire)
FROM
base_gid g
WHERE
a.num_version = g.num_version_contrat
),
a.compte_client = (
SELECT
MAX(g.compte_client)
FROM
base_gid g
WHERE
a.num_version = g.num_version_contrat
)
Can you see if there is an error ? If not, is there any way to debug the query ?
I don't know exactly why your update is non performant, but given the number of correlated subqueries you have, I'm not surprised. Try rewriting it as an update join:
UPDATE base_contrats_actifs a
INNER JOIN
(
SELECT
num_version_contrat,
MAX(code_indice) AS max_code_indice,
MAX(flag_bailleur_locataire) AS max_flag_bailleur_locataire,
MAX(compte_client) AS max_compte_client
FROM base_gid
GROUP BY num_version_contrat
) g
ON a.num_version = g.num_version_contrat
SET
a.code_indice = g.max_code_indice,
a.flag_bailleur_locataire = g.max_flag_bailleur_locataire,
a.compte_client = g.max_compte_client;

Operation with values in query inside stored procedure (mysql)

i have a stored procedure in mysql with a couple of queries and i need to perform some operations with that query.
This is some the code from the stored procedure:
BEGIN
SET ##session.collation_connection = ##global.collation_connection;
DROP TEMPORARY TABLE IF EXISTS innerContainers;
CREATE TEMPORARY TABLE `innerContainers` (
`id_container` INT(10) NOT NULL,
`display_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id_container`)
)
ENGINE = memory;
INSERT INTO innerContainers(id_container, display_name)
(SELECT c1.id_container, c1.display_name
FROM container_presentation cp
LEFT JOIN presentation p USING(id_presentation)
LEFT JOIN container c1 ON p.id_container = c1.id_container
WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
cp.id_container = in_id_container)
UNION
(SELECT c1.id_container, c1.display_name
FROM container_assembly_item cp
LEFT JOIN presentation p USING(id_presentation)
LEFT JOIN container c1 ON p.id_container = c1.id_container
WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
cp.id_container = in_id_container);
SELECT mad.id_container,
mat.sign_stock,
ma.id_management_start_point,
ma.id_management_end_point,
mad.quantity
FROM management_activity ma
LEFT JOIN management_activity_type mat ON ma.id_management_activity_type = mat.id_management_activity_type
LEFT JOIN management_activity_detail mad ON ma.id_management_activity = mad.id_management_activity
LEFT JOIN management_stock_point msp ON ma.id_management_end_point = msp.id_management_stock_point
LEFT JOIN management_stock_point msp1 ON ma.id_management_start_point = msp1.id_management_stock_point
WHERE mad.id_container IN (SELECT id_container FROM innerContainers)
ORDER BY mad.id_container ASC;
END
Now, after the last query.. i need to do some operations and return a value for each id_container inside the temporary table depending on the values in the second query.
Something like this:
foreach id_container in the second query i have a resultValue and i need to:
if the sign_stock == 1 and some other conditions then resultValue -= quantity and if sign_stock == 2 and some other conditions then resultValue += quantity.
And the final resultValue after iterating over the id_container lines will be the one i want for that id_container in the temporary table.
I dont know how to do that operation.. can some one help me with that?
Don't create a temporary table unless you need the data after the procedure call. Either way, in order to iterate over the results of a SELECT query, use a CURSOR.
A simple example is provided in the linked manual page.

mysql add key does not work

I have this query:
SELECT adressid, adressname FROM kino_adressen WHERE city ='Seattle'
I wanted to create an index like this
ALTER TABLE <tablename> ADD KEY index_abc(adressid, adressname(40))
But when I then check it by using:
EXPLAIN SELECT adressid, adressname FROM kino_adressen WHERE city ='Seattle'
It says
type = ALL
possible keys = NULL
key = NULL
...rows = 106
Can anyone give some piece of advice how to do this properly ?
// edit:
Another problem I do not understand:
SELECT DISTINCT
titel,
regie,
darsteller,
filmbild,
kino_filme.filmid,
kino_filme.beschreibung,
fsk,
filmlaenge,
verleih,
sprachfassung
FROM
kino_filme
LEFT JOIN kino_terminefilme ON (
kino_terminefilme.filmid = kino_filme.filmid
)
LEFT JOIN kino_termine ON (
kino_terminefilme.terminid = kino_termine.terminid
)
LEFT JOIN kino_kinos ON (
kino_kinos.kinoid = kino_termine.kinoid
)
LEFT JOIN kino_adressen ON (
kino_adressen.adressid = kino_kinos.adressid
)
WHERE
kino_adressen.adressid = 32038
And the result is like:
Why is kino_termine not using any index ?
I set it to PK while creating and even added an index afterwards, but none of those helped.
You added an index on the address but use the city in the where clause. Add an index on the city then it will be used.

MySQL view returns more results with an AND?

When I do this:
select * from vw_active_employees where division IS NULL; --319 results
Makes sense. And, then I do this...
select * from vw_active_employees where division IS NULL AND udds IS NULL; -- over 1000 results
Whaaaaat?? MORE results??
So, I had to wrap the view as a subselect for it to return what I was expecting. Like this:
select vw_active_employees.* from (select * from vw_active_employees) tmp where division IS NULL AND udds IS NULL; --317 results
Can someone explain this? I've never had to do this in MS SQLSERVER - so this is foreign to me.
The SQL used to create this view actually joins a number of other views together. I'm not entirely sure it's helpful to see, but you asked for it :) It's basically
CREATE VIEW `vw_active_employees` AS
select
e.*,
`vw_employee_attributes_map`.`med_school_faculty`, `vw_employee_attributes_map`.`paid_clinic_faculty`, `vw_employee_attributes_map`.`volunteer_clinic_faculty`, `vw_employee_attributes_map`.`dept_vote_rights`, `vw_employee_attributes_map`.`emeritus`, `vw_employee_attributes_map`.`aoa_member`, `vw_employee_attributes_map`.`faculty_senate`, `vw_employee_attributes_map`.`faculty_senator_elect`, `vw_employee_attributes_map`.`faculty_senator_alt_elect`, `vw_employee_attributes_map`.`exec_comm_member`, `vw_employee_attributes_map`.`ucc`, `vw_employee_attributes_map`.`icc`, `vw_employee_attributes_map`.`va`, `vw_employee_attributes_map`.`uwmf`, `vw_employee_attributes_map`.`affiliate`, `vw_employee_attributes_map`.`aurora`, `vw_employee_attributes_map`.`website_searchable`,
`vw_employee_current_appointment_info`.`termination_date`, `vw_employee_current_appointment_info`.`last_promotion_date`, `vw_employee_current_appointment_info`.`promotion_due_date`, `vw_employee_current_appointment_info`.`last_reappointment_date`, `vw_employee_current_appointment_info`.`reappointment_duration`, `vw_employee_current_appointment_info`.`reappointment_due_date`, `vw_employee_current_appointment_info`.`non_renewal_date`, `vw_employee_current_appointment_info`.`roster`, `vw_employee_current_appointment_info`.`payroll`, `vw_employee_current_appointment_info`.`on_probation`, `vw_employee_current_appointment_info`.`probation_complete`, `vw_employee_current_appointment_info`.`probation_notify_sent_date`, `vw_employee_current_appointment_info`.`probation_end_date`, `vw_employee_current_appointment_info`.`uw_appointment_id`, `vw_employee_current_appointment_info`.`effective_date`, `vw_employee_current_appointment_info`.`percent`, `vw_employee_current_appointment_info`.`end_date`, `vw_employee_current_appointment_info`.`continuity_status`, `vw_employee_current_appointment_info`.`guaranteed_length`, `vw_employee_current_appointment_info`.`end_reason`, `vw_employee_current_appointment_info`.`midterm_eval_received`, `vw_employee_current_appointment_info`.`final_eval_received`, `vw_employee_current_appointment_info`.`annual_eval_letter_sent`, `vw_employee_current_appointment_info`.`annual_eval_sent_date`, `vw_employee_current_appointment_info`.`annual_eval_received`, `vw_employee_current_appointment_info`.`annual_eval_received_date`, `vw_employee_current_appointment_info`.`probation_month_done`, `vw_employee_current_appointment_info`.`seniority_date`, `vw_employee_current_appointment_info`.`represented`, `vw_employee_current_appointment_info`.`work_schedule`, `vw_employee_current_appointment_info`.`evaluation_end_date`, `vw_employee_current_appointment_info`.`evaluation_month_end`, `vw_employee_current_appointment_info`.`evaluation_sent_date`, `vw_employee_current_appointment_info`.`evaluation_completed`, `vw_employee_current_appointment_info`.`hiring_pi`, `vw_employee_current_appointment_info`.`appointment_type`, `vw_employee_current_appointment_info`.`appointment_type_code`, `vw_employee_current_appointment_info`.`appointment_classified`, `vw_employee_current_appointment_info`.`termination_type`, `vw_employee_current_appointment_info`.`head`, `vw_employee_current_appointment_info`.`secretary`, `vw_employee_current_appointment_info`.`division`, `vw_employee_current_appointment_info`.`supervisor_first_name`, `vw_employee_current_appointment_info`.`supervisor_last_name`, `vw_employee_current_appointment_info`.`title`, `vw_employee_current_appointment_info`.`title_code`, `vw_employee_current_appointment_info`.`udds`, `vw_employee_current_appointment_info`.`udds_code`,
`vw_employee_current_background_info`.`visa_holder`, `vw_employee_current_background_info`.`visa_type`, `vw_employee_current_background_info`.`visa_expiration_date`, `vw_employee_current_background_info`.`license`, `vw_employee_current_background_info`.`license_exp_date`, `vw_employee_current_background_info`.`dea`, `vw_employee_current_background_info`.`dea_expiration_date`, `vw_employee_current_background_info`.`national_provider_number`, `vw_employee_current_background_info`.`residency_location`, `vw_employee_current_background_info`.`residency_end_date`, `vw_employee_current_background_info`.`fellowship_location`, `vw_employee_current_background_info`.`fellowship_end_date`, `vw_employee_current_background_info`.`primary_board_cert`, `vw_employee_current_background_info`.`primary_cert_date`, `vw_employee_current_background_info`.`specialty_board_cert`, `vw_employee_current_background_info`.`specialty_cert_date`, `vw_employee_current_background_info`.`degree_info_reports`, `vw_employee_current_background_info`.`healthlink_id`, `vw_employee_current_background_info`.`uwmf_general_ledger_id`, `vw_employee_current_background_info`.`uwmf_employee_id`, `vw_employee_current_background_info`.`specialty`,
`vw_employee_current_compliance_info`.`hipaa_training_completed`, `vw_employee_current_compliance_info`.`sic_training_completed`, `vw_employee_current_compliance_info`.`background_check_completed`, `vw_employee_current_compliance_info`.`i9_completed`, `vw_employee_current_compliance_info`.`caregiver_applies`, `vw_employee_current_compliance_info`.`caregiver_check_completed`, `vw_employee_current_compliance_info`.`tb_completed`, `vw_employee_current_compliance_info`.`rubella_immunity_comfirmed`, `vw_employee_current_compliance_info`.`respiratory_test_completed`, `vw_employee_current_compliance_info`.`health_link`, `vw_employee_current_compliance_info`.`access_request_uwhc`, `vw_employee_current_compliance_info`.`rn_credentialing`, `vw_employee_current_compliance_info`.`bls_cert_expiration`, `vw_employee_current_compliance_info`.`uwhc_cred_submitted`,
`vw_employee_current_contact_info`.`employee_id`, `vw_employee_current_contact_info`.`ext_office`, `vw_employee_current_contact_info`.`area_code_office`, `vw_employee_current_contact_info`.`number_office`, `vw_employee_current_contact_info`.`country_code_office`, `vw_employee_current_contact_info`.`ext_home`, `vw_employee_current_contact_info`.`area_code_home`, `vw_employee_current_contact_info`.`number_home`, `vw_employee_current_contact_info`.`country_code_home`, `vw_employee_current_contact_info`.`address1_office`, `vw_employee_current_contact_info`.`address2_office`, `vw_employee_current_contact_info`.`city_office`, `vw_employee_current_contact_info`.`state_office`, `vw_employee_current_contact_info`.`zip_office`, `vw_employee_current_contact_info`.`mail_code_office`, `vw_employee_current_contact_info`.`address1_home`, `vw_employee_current_contact_info`.`address2_home`, `vw_employee_current_contact_info`.`city_home`, `vw_employee_current_contact_info`.`state_home`, `vw_employee_current_contact_info`.`zip_home`, `vw_employee_current_contact_info`.`email`
from employee e
left join vw_employee_attributes_map on e.id = vw_employee_attributes_map.employee_id
left join vw_employee_current_appointment_info on e.id = vw_employee_current_appointment_info.employee_id
left join vw_employee_current_background_info on e.id = vw_employee_current_background_info.employee_id
left join vw_employee_current_compliance_info on e.id = vw_employee_current_compliance_info.employee_id
left join vw_employee_current_contact_info on e.id = vw_employee_current_contact_info.employee_id
where e.active = 1
And the vw_employee_current_appointment_info which contains 'division' and 'udds' looks like:
CREATE VIEW `vw_employee_current_appointment_info` AS
select e.id as `employee_id`,
`appointment`.`termination_date`, `appointment`.`last_promotion_date`, `appointment`.`promotion_due_date`, `appointment`.`last_reappointment_date`, `appointment`.`reappointment_duration`, `appointment`.`reappointment_due_date`, `appointment`.`non_renewal_date`, `appointment`.`roster`, `appointment`.`payroll`, `appointment`.`on_probation`, `appointment`.`probation_complete`, `appointment`.`probation_notify_sent_date`, `appointment`.`probation_end_date`, `appointment`.`uw_appointment_id`, `appointment`.`effective_date`, `appointment`.`percent`, `appointment`.`end_date`, `appointment`.`continuity_status`, `appointment`.`guaranteed_length`, `appointment`.`end_reason`, `appointment`.`midterm_eval_received`, `appointment`.`final_eval_received`, `appointment`.`annual_eval_letter_sent`, `appointment`.`annual_eval_sent_date`, `appointment`.`annual_eval_received`, `appointment`.`annual_eval_received_date`, `appointment`.`probation_month_done`, `appointment`.`seniority_date`, `appointment`.`represented`, `appointment`.`work_schedule`, `appointment`.`evaluation_end_date`, `appointment`.`evaluation_month_end`, `appointment`.`evaluation_sent_date`, `appointment`.`evaluation_completed`, `appointment`.`hiring_pi`,
`appointment_type`.`name` as `appointment_type`, `appointment_type`.`code` as `appointment_type_code`, `appointment_type`.`classified` as `appointment_classified`,
`termination`.`name` as `termination_type`,
`appointment_division`.`head`, `appointment_division`.`secretary`,
`division`.`division_name` as `division`,
`supervisor`.`first_name` as `supervisor_first_name`, `supervisor`.`last_name` as `supervisor_last_name`,
`title`.`name` as `title`, `title`.`code` as `title_code`,
`udds`.`name` as `udds`, `udds`.`code` as `udds_code`
from employee e
left join appointment on appointment.employee_id = e.id and appointment.primary = 1
left join appointment_type on appointment.appointment_type_id = appointment_type.id
left join termination on appointment.termination_id = termination.id
left join appointment_division on appointment_division.appointment_id = appointment.id and appointment_division.primary = 1
left join division on appointment_division.division_id = division.id
left join appointment_supervisor on appointment_supervisor.appointment_id = appointment.id and appointment_supervisor.primary = 1
left join employee supervisor on appointment_supervisor.supervisor_id = supervisor.id
left join appointment_title on appointment_title.appointment_id = appointment.id and appointment_title.primary = 1
left join title on appointment_title.title_id = title.id
left join appointment_udds on appointment_udds.appointment_id = appointment.id and appointment_udds.primary = 1
left join udds on udds.id = appointment_udds.udds_id
you have used left join in your tables .when join condition is true than it has been select records those match in all tables than fetch records from left tables one by one and in this case your null condition is true and it is repeat the result set view
You are using left joins so your base table connects to the subsequent tables even if no value exists. However, when you begin to filter the subsequent tables you are specifying a that the subsequent table contain certain values. In this case there are probably only 319 values in the "vw_employee_current_appointment_info" with a null in the division field. When you add in the udds field there can be more fields that are null, because of the left join the division field is NULL by definition
So, the number of connections where "vw_employee_current_appointment_info" has udds as NULL is greater than the number of records where division is NULL, so it brings them all back. If you try this with equal joins then the number will be 319 max.