A SQL query retrieve same line several time - mysql

I create a web page to display information from a database. The recovered data comes from five different tables. However, the SQL query sends the same line several times (5900 lines in the DB, 24000 on the page).
I tried with the classic JOIN, LEFT JOIN and LEFT OUTER JOIN but I still get the same result.
Here is my MySQL query:
SELECT
vs.Nom_VS,
vs.IP_VS,
vs.Port_VS,
f5.Cluster_F5,
pa.Nom_partition,
po.Nom_pool,
me.IP_membre,
Port_membre
FROM VS_F5 vs
JOIN F5 f5 ON (f5.id_F5 = vs.id_F5)
JOIN Partition pa ON (pa.id_partition = vs.id_partition)
JOIN Pool po ON (po.id_pool = vs.id_pool_pool)
JOIN Membres_F5 me ON (me.id_pool_pool = vs.id_pool_pool)
As I said, I retrieve 24000 lines while the DB only contains 5900 lines. I really don't understand why I get the same result with different JOIN.
Hope you can help (and sorry if this is a stupid mistake).

Ok that was just a problem in the db. Sorry

Related

Problems with a LEFT JOIN

I'm trying to do a left join in two tables, the principal one is a table with production orders and the other one contains the quantity of defective material in those production orders. It is possible to have more than one different type of defect in one production order, so that means more than one register in the second table for one production order. The relations between table are the field production order, machine, production phase, article and enterprise:
SELECT PM.FECHA_FABRICACION,
--TRUNC(PM.FECHA_FABRICACION) AS FECHA,
PM.ORDEN_DE_FABRICACION,
PM.CODIGO_FAMILIA,
PM.CODIGO_ARTICULO,
PM.COD_MAQUINA,
DECODE (PM.COD_MAQUINA,'AN001','ANODIZADO', 'GR001','ANODIZADO', 'ES001','ANODIZADO','PU001', 'ANODIZADO', 'ZZ141', 'ANODIZADO', PM.COD_MAQUINA) AS MAQUINA_PARTE,
PM.DESC_MAQUINA,
PM.CANTIDAD_ACEPTADA,
PM.M2_ACEPTADOS,
PM.M2_CONPEPTO,
PM.M2_TOTAL,
PM.M2_EXT,
PM.KILOS_ACEPTADOS,
PM.BARRAS_ACEPTADAS,
PM.FASE_REALIZADA,
PR.CODIGO_DEFECTO,
PR.CANTIDAD_RECHAZADA,
PR.LONGITUD,
PR.KILOS_RECHAZADOS,
PR.OBSERVACIONES
FROM ST_VW_PRODUCCION_MAQUINAS PM
LEFT JOIN P_INFO_RECHAZOS PR
ON PM.CODIGO_EMPRESA = PR.CODIGO_EMPRESA
AND PM.ORDEN_DE_FABRICACION = PR.ORDEN_DE_FABRICACION
AND PM.CODIGO_ARTICULO = PR.CODIGO_ARTICULO
AND PM.COD_MAQUINA = PR.CODIGO_MAQUINA
AND PM.FASE_REALIZADA = PR.FASE
AND PM.CODIGO_EMPRESA = '01'
AND PM.FECHA_FABRICACION > TO_DATE('04/07/2022 00:00:00', 'DD/MM/YYYY HH24:MI:SS')
--AND TRUNC(PM.FECHA_FABRICACION) = TRUNC(PR.FECHA_RECHAZO);
However it's not working
If you are specifically looking for rejections, you dont need a LEFT JOIN, but a normal INNER JOIN, meaning you ONLY WANT to see those projects where at least one, or more, rejections had occurred.
The query itself looks ok otherwise, just get rid of the LEFT clause of left join.
As for the function DECODE, that is using PM.COD_MAQUINA at both the begin and end of the list of codes. Dont know if that was intentional.
For date filtering, if you are only looking for a specific date without regard to any time portion, you could change it to YYYY-MM-DD format without using the TO_DATE() construct as in
AND PM.FECHA_FABRICACION > '2022-07-04'
If you still are getting errors, please EDIT your post and provide what the error message is.
To explain further, you are using a LEFT JOIN, which is a type of outer join. An outer join means that all the results from one side that don't match will still be displayed, and will just be matched to null. In your case, production orders that aren't defective will be listed.
However, it seems that you want to only display results that match on both sides (production orders that do have defects). When you just JOIN then you will have the result you want.
Here's a helpful diagram

phpMyadmin Query Execution Time

I tried to run the following query in my Cloud VPS cPanel phpMyadmin
SELECT bankcode, bankname
FROM newbankdetails
WHERE
type='DB' AND
bankcode IN ( SELECT drawingbankname
FROM dd1
WHERE entrydate BETWEEN '01/04/2014' AND '30/04/2014'
AND paymentmode='DD'
AND state='TAMIL NADU' )
It takes very very very long time nearly 5 to 6 hours and says out of time or keeps running for days and don't show any error also don't show results.
Whereas it works perfectly in my local machine xampp
It happens only when working on large size tables like 12GB and around like that
How to speed it up and display the result as it displays instantly in localhost xampp
I think, replacing IN with a LEFT JOIN can make more sense, like this:
SELECT
bankcode, bankname
FROM
newbankdetails nd
LEFT JOIN
dd1 ON nd.bankcode = dd1.drawingbankname
WHERE
nd.type='DB'
AND
dd1.entrydate BETWEEN '01/04/2014' AND '30/04/2014'
AND
dd1.paymentmode = 'DD'
AND
dd1.state = 'TAMIL NADU'
I can't test it, But I think with dd1.paymentmode = 'DD' automatically removes rows of dd1.paymentmode IS NULL because of using LEFT JOIN.
First you should use EXPLAIN QUERY (https://dev.mysql.com/doc/refman/5.0/en/using-explain.html)
That will give you information about what is slow.
Your problem seems to be related to the size of the table. On your localhost you may have a small table, but in production it is very large. Also put an index on the first table on nd.bankcode.
You should try to remove the subselect, and use a JOIN instead.
Also, you should put indexes on the columns you use for search.
Put an index on drawingbankname, entrydate, paymentmode, state. Put also an index on nd.backcode .
Column drawingbankname is varchar, you should convert it to fixed char.
Remove the subselect and use JOIN.
Use EXPLAIN after the changes to see progress.

Exporting phpmyadmin query as csv - problems (not a duplicate)

Before anyone says this is a duplicate, I havent found this case anywhere.
I make my query (a joined query with 4 tables) and then hit EXPORT, but it exports just the table and not the query with the combined info and not just the fields I asked for but ALL the fields of the first table in the query. So basically, the query is ignored at the export stage.
I have loooked into "INTO OUTFILE" however, I do not have the right access to access the file after it is created.
I wonder if "INTO OUTFILE" can save files elsewhere? (remotely, I mean)
I wonder if the amount of results (6200( is straining the server?
My code :
SELECT pname, concat(afname, ' ', alname) as artistname, pubname, pwide, phigh, concat(pubcode,'-',psku) as fullsku, catname, concat('/prints/', pubcode,'-',psku, '.jpg') as imagesource, ptext FROM (prints LEFT JOIN publishers ON (publishers.pubid=prints.ppub) LEFT JOIN artists ON (artistid=partist)) LEFT JOIN cats ON (pcat = catid) WHERE psku <> '' AND phigh > 0 ORDER BY pubname,ltrim(alname),ltrim(afname) LIMIT 6200
I have the same problem, when hitting the "export" button at the bottom of my query results, phpmyadmin exports one of the tables, not the query results. I can't figure why. My workaround is to create a new table, adding
CREATE TABLE xxxx AS
before my select query, and then export this new table.
Since I haven't heard anything, I will post my very lame workaround but at least I got what I wanted.
I did a copy/paste into BBEdit and then tidied up the data with a series of find/replaces.
Not ideal and not the quickest solution, but it worked where nothing else would.

MultipleObjectsReturned error in django admin - but there is no duplication in database

I'm having a problem in the Django admin. I'm using version 1.5.5
I have a Booth model which has a foreign key to my AreaLayout model, which then goes back through another few models with foreign and many2many keys. My model code can be seen on pastebin. The initial indication of the problem in admin is that AreaLayouts are being duplicated in the select dropdown in Booth admin. The MultipleObjectsReturned error (traceback) is being raised when I then try to save a new booth. I was able to trace this back to the SQL query that django is creating to grab the AreaLayout list:
SELECT `venue_arealayout`.`id`, `venue_arealayout`.`name`, `venue_arealayout`.`description`, `venue_arealayout`.`area_id`, `venue_arealayout`.`additional_notes`
FROM `venue_arealayout`
INNER JOIN `venue_area` ON (`venue_arealayout`.`area_id` = `venue_area`.`id`)
INNER JOIN `venue_venue` ON (`venue_area`.`venue_id` = `venue_venue`.`id`)
INNER JOIN `venue_venue_venue_type` ON (`venue_venue`.`id` = `venue_venue_venue_type`.`venue_id`)
INNER JOIN `venue_venuetype` ON (`venue_venue_venue_type`.`venuetype_id` = `venue_venuetype`.`id`)
WHERE (`venue_arealayout`.`id` = 66 )
This query produces a duplicate in MySQL when I run it there. Removing the final 2 JOINs results in a single result being returned (which is the desired result), whereas removing only the last join still results in duplication.
I tried running that query with SELECT * in place of selecting specific fields and the two results in that case are almost equal. The difference is that the venue in question has multiple venuetypes and I'm getting a result for each of those. Is there any way I can tell django not to include those joins for these queries, or is there a way I can get distinct results as far as AreaLayouts go?
I think you're being caught by the bug reported in ticket 11707. One of the comments mentions that it can cause a MultipleObjectsReturned exception.
All I can suggest is that you stop using limit_choices_to. Your models are fairly complex, so I can't immediately see which one is causing the problem.

Mysql OR query with a join returns an out of memory error

I have a query that tries to tell is a member had participated in any of 3 types of actions. Each action is represented by a table that I am joining. And then I am doing an or to see if there is a match in any of the tables. Here is the query:
select problems.problem_id, problem_title from
problems left join attempted_solutions
on roblems.creator_member_id = attempted_solutions.suggester_id
left join problem_comments
on problems.creator_member_id = problem_comments.user_id
left join suggested_solutions
on problems.creator_member_id = suggested_solutions.suggester_id
where attempted_solutions.suggester_id = 1
or problem_comments.user_id = 1 or suggested_solutions.suggester_id = 1;
But for some reason this gives a "MySQL client ran out of memory error." if I try it command-line, and a "database connection interrupted" error if I do it from a script.
Any idea where I am going wrong here?
This thread has some solutions maybe you should try them see if any of them is your problem
http://forums.mysql.com/read.php?37,94800,232288#msg-232288
also, try to simplify your query then add your joins and where statements back one by one so that you can detect the exact cause of your problem