mysqli select over two tables - speed optimisation - mysql

SQL-Select:
SELECT mf.*
, ma.Titel
FROM `sporttype_fragebogen` as mf
, sporttypen_auswertung as ma
WHERE mf.SessionID = ma.SessionID
ORDER
BY mf.ID
In both tabels there are around 17'000 data set. My problem is, that it takes quite long to the the results. How can i speed-optimise my SQL Select?
I also tried with a join, but same "Problem":
SELECT *
FROM `sporttype_fragebogen`
JOIN sporttypen_auswertung
ON sporttype_fragebogen.SessionID = sporttypen_auswertung.SessionID
ORDER
BY sporttype_fragebogen.ID
Thanks for help

Related

How to optimize a query that is a join of 2 very similar queries but with different aggregation

I have the following query:
SELECT OBJ_DESC_ERRORS.description, OBJ_DESC_ERRORS.object, OBJ_DESC_ERRORS.count_errors, OBJ_ERRORS.count_total FROM
(SELECT `metrics_event`.`description`, `metrics_event`.`object`, COUNT(`metrics_event`.`id`) AS `count_errors` FROM `metrics_event`
INNER JOIN `metrics_session` ON (`metrics_event`.`session_id` = `metrics_session`.`id`)
WHERE (`metrics_session`.`training_id` = 4 AND NOT (`metrics_session`.`completed_at` IS NULL) )
GROUP BY `metrics_event`.`description`, `metrics_event`.`object` ORDER BY `count_errors` DESC ) as OBJ_DESC_ERRORS
JOIN
(SELECT `metrics_event`.`object`, COUNT(`metrics_event`.`id`) AS `count_total` FROM `metrics_event`
INNER JOIN `metrics_session` ON (`metrics_event`.`session_id` = `metrics_session`.`id`)
WHERE (`metrics_session`.`training_id` = 4 AND NOT (`metrics_session`.`completed_at` IS NULL) )
GROUP BY `metrics_event`.`object` ORDER BY `count_total` DESC ) as OBJ_ERRORS
ON OBJ_DESC_ERRORS.object = OBJ_ERRORS.object
which produces the following result:
As you can see I'm basically running the same query twice. The reason for that is that I need to have that count_errors broken down by each aggregation of object + description, but I also need the count_total to be only aggregated by object. This was the way I could think of. Now I'd like to know if this is the best I can do or if it can be optimized even further.
If so I have no clue how. Googling and searching similar topics on this is difficult because the optimization task depends on the query itself, so keywords here didn't help me much.
Get rid of the inner ORDER BYs; they do nothing useful.
Rewrite the query something like this:
SELECT
me.description,
me.object,
SUM(...) AS count_errors,
SUM(...) AS count_total
FROM `metrics_event` AS me
INNER JOIN `metrics_session` AS ms ON (me.`session_id` = ms.`id`)
WHERE ms.`training_id` = 4
ms.`completed_at` IS NOT NULL
GROUP BY me.`description`, me.`object`
ORDER BY `count_total` DESC
Since a boolean expression evaluates as 1 for TRUE, else 0, devise the argument to SUM() to be a boolean expression that provides the desired COUNT.

Perform a SQL Query

I have this query in PHP MySQL PDO:
SELECT p.las_plano_id, p.mensalidade_diferenciada, v.las_tipos_planos_id, t.valor_mensalidade
FROM isw_planos AS p
INNER JOIN isw_planos_vinculos AS v
ON p.las_plano_id =
(SELECT v.las_plano_id
FROM isw_planos_vinculos
WHERE v.data_encerramento IS NULL
ORDER BY v.data_adesao
DESC LIMIT 1)
INNER JOIN isw_planos_tipos AS t
ON v.las_tipos_planos_id = t.id
WHERE p.ativo = 1
But.. the result generate a long delay.. it's possible to perform this query to execute more fast?
Thnaks..
I suspect the error is with v.:
This looks wrong: SELECT v.las_plano_id ... since v is outside the subquery. Please check the aliases used.
If removing v. does not help, please provide SHOW CREATE TABLE so we can see the indexes, etc.

Sql Join query- setting default value if NOT in join query

I have a SQL query that links 2 tables to provide the data if a horse is in both the tables:
SELECT ProformSystem.TheDate as racedate,
ProformSystem.Course as course,
ProformSystem.TheTime as thetime,
ProformSystem.Horse as horse,
ATRSpeedRatings.rank as rank
FROM ATRSpeedRatings
INNER JOIN ProformSystem ON (ATRSpeedRatings.Horse = trim(ProformSystem.Horse)) AND (ATRSpeedRatings.TheDate = ProformSystem.TheDate) order by ProformSystem.TheTime;"
Is it possible that if the horse in ProformSystem.Horse is NOT in ATRSpeedRatings.Horse then I just make rank = 0 as a default value, or would I need to run a separate query?
This way I can display all horses from ProformSystem even if they don't have a rank in ATRSpeedRatings.
I think you want a left join and coalesce():
SELECT ps.TheDate as racedate, ps.Course as course, ps.TheTime as thetime,
ps.Horse as horse, COALESCE(sr.rank, 0) as rank
FROM ProformSystem ps LEFT JOIN
ATRSpeedRatings sr
ON sr.Horse = TRIM(ps.Horse) AND sr.TheDate = ps.TheDate)
ORDER BY ps.TheTime;
Note that this query uses table aliases. These make the query easier to write and to read.
Also, the JOIN condition sr.Horse = trim(ps.Horse) is highly suspect. You should fix the data so there are no spaces in ProformSystem. Fixing the data is more efficient and it will prevent problems on future queries.

Multiple On conditions in Doctrine2 Left Join

I have RATE and BRANCH_CURRE table. I want to perform left join operation (joining branch to rate) in Doctrine Query Language (DQL).
My SQL Query is:
SELECT r.id rid
,r.TIME rtime
,r.rate_candidate
,r.exchange_rate
,r.branch
,r.STATUS ratestatus
,bc.currency
,bc.scale bcscale
,bc.STATUS bcstatus
FROM rate r
LEFT JOIN branch_currency bc ON (
r.branch = bc.branch
AND (
r.from_currency = bc.currency
OR r.to_currency = bc.currency
)
)
WHERE r.STATUS = 1
AND bc.STATUS = 1;
To be more specific, I have two questions here
How to select some specific columns from both the tables.
How to give the multiple ON conditions while joining tables.
So Please show the DQL query using queryBuilder(). Thanx in advance!!!
I suggest to add the additional conditions into a where condition.
Other than that I highly recommend to read the documentation regarding the Doctrine QueryBuilder etc. because you're question does not show that you have any experience with Doctrine at all. Just throwing a MySQL query without any personal effort at us is not a nice and fair way.
This is not tested but should give you some guidance.
$qb = $this->_em->createQueryBuilder();
$qb->select('r.branch, bc.exchange_rate');
$qb->from('rate', 'r');
$qb->leftJoin('r.branch', 'bc');
$qb->where($qb->expr()->orX('r.from_currency=bc.currency','r.to_currency = bc.currency));

need help in optimizing mysql query

i am writing a mysql query as below
SELECT `user_master`.`first_name`,
`city_name`,
`user_master`.`last_name`,
`user_master`.`user_master_id`,
`account_management_master`.`account_name`,
`donation_receipt_info`.`receipt_temple_id`,
date(dt) AS dt,
SUM(`donation_receipt_info`.`amount`) AS amount
FROM (`donation_receipt_info`)
JOIN `donation_receipt_master` ON donation_receipt_master`.`receipt_id`=`donation_receipt_info`.`receipt_id`
JOIN `account_management_master` ON `account_management_master`.`account_id`=`donation_receipt_info`.`account_id`
JOIN `user_master` ON `user_master`.`user_master_id`=`donation_receipt_master`.`user_master_id`
JOIN `user_address_info` ON `user_address_info`.`user_master_id`=`user_master`.`user_master_id`
JOIN `city_master` ON `city_master`.`city_id`=`user_address_info`.`city_id`
WHERE `donation_receipt_info`.`temple_id` = '1'
GROUP BY `donation_receipt_info`.`receipt_id`,
`donation_receipt_info`.`account_id`
the table donation_receipt_info and master have approx 42k results the query is taking way to much time of about 5 to 6 minutes to execute in mysql itself.
can someone please help me optimize the query, any help or suggestion would be very helpful
Thanks.
First, your query is impossible to read. You should format it and learn to use table aliases:
SELECT um.first_name, city_name, um.last_name, um.user_master_id, amm.account_name,
dri.receipt_temple_id, date(dt) AS dt, SUM(dri.amount) AS amount
FROM donation_receipt_info dri JOIN
donation_receipt_master drm
ON drm.receipt_id = dri.receipt_id JOIN
account_management_master amm
ON amm.account_id = dri.account_id JOIN
user_master um
ON um.user_master_id = drm.user_master_id JOIN
user_address_info uai
ON uai.user_master_id = um.user_master_id JOIN
city_master cm
ON cm.city_id = uai.city_id
WHERE dri.temple_id = '1'
GROUP BY dri.receipt_id, dri.account_id;
Next. Do all the tables have the obvious indexes? That is, each table appears to have an id and these should be declared as keys (primary keys preferably). For instance, city_master(city_id).
Next, there should be an index on donation_receipt_info(temple_id, receipt_id, account_id). This should help with the where. Note: if temple_id is really an integer, the where clause should be expressed as WHERE dri.temple_id = 1 -- no quotes. You don't want MySQL to get confused and decide not to use the index.
These changes will probably help. 5-6 minutes seems like a long time for such a query.