how to convert simple mysql query to doctrine Query language? - php

Convert mysql Query to doctrine Query language ?
I have done the mysql and run the query in phpmyadmin and I'm getting returned rows :
select ppp.payperiod_sdesc,ppesa.gross_pay,pptpp.esi_employer_contribution,pptpp.pf_employer_contribution,pplw.employerContribution
from py_process_emp_status_approved AS ppesa
left join py_process_tds_pf_pt AS pptpp on ppesa.ou_code = pptpp.ou_code
left join py_pay_group AS ppg on pptpp.pg_code = ppg.pg_code
left join py_process_labour_welfare AS pplw on ppg.pg_code = pplw.pg_code
left join py_pay_period AS ppp on pplw.payperiod_code = ppp.payperiod_code
left join py_payroll_calendar AS ppc on ppp.paycal_code = ppc.paycal_code
WHERE ppesa.ou_code = 15000
ORDER BY ppesa.ou_code DESC
LIMIT 0,5
please convert the mysql query to doctrine Query language.

You may be able to find more guidance in the docs here:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/native-sql.html
Have a look at result set mapping. You can also just create a repository function to execute native sql and then call your repo function.
You can then call something like this below:
results = getDoctrine()->getManager()->getRepository(Your repo)->repofunction();
If your unsure how to create / call a repository function its covered multiple times in the symfony docs.
Doctrine loads all results then filters down, this is an expensive process you may be best letting sql do the work for you.
Hopefully you have everything you need to get to your end result.

Related

check if query builder already issued where or like?

I am so convenient using built-in codedigniter query builder.
I have to output searching to an existing page since that page involves multi-table.
Currently, I already have a lot of if there,
I am curious how to check if statement db->where() or db->like() have been called or not?
My second option is to construct new query that join 2 tables, but my table does not have any direct relationship, I try to use $this->db->join() it fails on the join condition. I am lack of example of using join, especially the join type. I am expecting to use something like
SELECT * FROM p JOIN hs ON hs.id=%-p.id-%
I am already stuck here, and I am aware that the db->get() will erase the data filtering.
Any suggestion?

How to know what is slowing down a MySql left join

I created a view that is a join between two tables a.2_9 (18.483 rows) and b.2_10 (5700 rows)
it is a very simple join:
Create view v_2_9_join_2_10 AS
Select *
FROM 2_9 AS a
LEFT JOIN 2_10 b
ON a.2_9_tid = b.2_10_tid
AND a.2_9_region = b.2_10_region
AND a.n_2 = b.n_2
I to create the view and count the rows (18483) but for some reason it takes forever when I try to use the view, count or export or just browse and it is slowing down the database in PHP too much.
Any ideas of how I can look for the reason(s) of the slowing down or optimize the join?
To know why a query is being slow you can issue the query you used to create the view with the EXPLAIN keyword, this will show a little table with data that explains how MySQL executed the query so you can identify the bottlenecks there.
HERE you can find a little tutorial on how to understand the output of EXPLAIN.

Slow Oracle query

I am building a web application for a customer and I have a problem with a particular SQL query.
The query is:
select order_header.order_no,
order_header.purchase_order_no,
order_header.entry_date,
order_header.delivery_date,
order_totals.total_quantity
from order_header,
order_totals
where order_header.order_no = order_totals.order_no
I have done some troubleshooting and this:
where order_header.order_no = order_totals.order_no
is the problem. The SQL query with this line takes 35 seconds (causes DataTables to even time out at times) and without it it is instant.
So, I know the problem but I'm not a DBA so don't know the solution.
It's not my database, so the solution I need to send to the DBA to sort so I can continue with my job. Something like
"Hey, would you mind doing A on B table so that C speeds up?"
I just don't know what actually needs to be done!
First add an index on both order_header.order_no and order_totals.order_no and check that both the columns are of the same type.
For other optimizations we should talk about the data.
Don't forget to update the statistics
Ask your DB to
add an index on the order_no column in both order_header and order_total
That should help with your current problem. Also, look up JOIN and change your query to use the JOIN syntax.
select order_header.order_no,
order_header.purchase_order_no,
order_header.entry_date,
order_header.delivery_date,
order_totals.total_quantity
from order_header
join order_totals ON order_header.order_no = order_totals.order_no

Paginator with "$fetchJoinCollection = true" won't respect "ORDER BY" in doctrine DQL?

Having a strange issue. We are using MariaDB 5.5 and doctrine/orm 2.3.3, and trying to use the Doctrine Paginator with DQL.
http://docs.doctrine-project.org/en/latest/tutorials/pagination.html
The DQL has an ORDER BY clause [see below for an illustration example]. However, the result is not sorted at all for a given page size. And, if we increase the page size to cover the entire result set, the sorting becomes correct.
$dql = "SELECT a, b FROM EntityA a JOIN a.propertyB b ORDER BY a.createdOn DESC";
$query = $this->em->createQuery($dql)
->setMaxResults($pageSize)
->setFirstResult($offset);
$paginator = new Paginator($query, $fetchJoinCollection=true);
....
I dumped the sql and manually ran it. The sql also gave the correct sorting. So something is causing the sorting issue inside Doctrine's Paginator class.
When I set $fetchJoinCollection=false and passed it to the Paginator constructor, the sorting became correct for any given $pageSize!
Read Doctrine source code [Doctrine/ORM/Tools/Pagination/Paginator.php]. With $fetchJoinCollection=true, doctrine uses a WhereInWalker to get the final result, which doesn't respect the ORDER By clause in the DQL, because the IN() clause doesn't generate the result in the same order as the ids inside the IN() clause.
A sorting solution for the IN() clause can be found in Ordering by the order of values in a SQL IN() clause. But I can't find Doctrine using that.
Anyone with Doctrine internal knowledge would shed some light?! Thanks!
Found out that people have taken care of this issue already.
http://www.doctrine-project.org/jira/browse/DDC-2593

Making an SQL query more efficient

I have a query that works, but it's taking at least 3 seconds to run so I think it can probably be faster. It's used to populate a list of new threads and show how many unread posts there are in each thread. I generate the query string before throwing it into $db->query_read(). In order to only grab results from valid forums, $ids is string with up to 50 values separated by commas.
The userthreadviews table has existed for 1 week and there are roughly 9,500 rows in it. I'm not sure if I need to set up a cron job to regularly clear out thread views more than a week old, or if I will be fine letting it grow.
Here's the query as it currently stands:
SELECT
`thread`.`title` AS 'r_title',
`thread`.`threadid` AS 'r_threadid',
`thread`.`forumid` AS 'r_forumid',
`thread`.`lastposter` AS 'r_lastposter',
`thread`.`lastposterid` AS 'r_lastposterid',
`forum`.`title` AS 'f_title',
`thread`.`replycount` AS 'r_replycount',
`thread`.`lastpost` AS 'r_lastpost',
`userthreadviews`.`replycount` AS 'u_replycount',
`userthreadviews`.`id` AS 'u_id',
`thread`.`postusername` AS 'r_postusername',
`thread`.`postuserid` AS 'r_postuserid'
FROM
`thread`
INNER JOIN
`forum`
ON (`thread`.`forumid` = `forum`.`forumid`)
LEFT JOIN
(`userthreadviews`)
ON (`thread`.`threadid` = `userthreadviews`.`threadid`
AND `userthreadviews`.`userid`=$userid)
WHERE
`thread`.`forumid` IN($ids)
AND `thread`.`visible`=1
AND `thread`.`lastpost`> time() - 604800
ORDER BY `thread`.`lastpost` DESC LIMIT 0, 30
An alternate query that joins the post table (to only show threads where user has posted) is actually twice as fast, so I think there's got to be something in here that could be changed to speed it up. Could someone provide some advice?
Edit: Sorry, I had put the EXPLAIN in front of the alternate query. Here is the correct output:
As Requested, here is the output generated by EXPLAIN SELECT:
Have a look at the mysql explain statement. It gives you a execution plan of your query.
Once you know the plan, you can check if you have got a index on the fields involved in the plan. If not, create them.
Perhaps the plan reveals details about how the query can be written in another way, such that the query will be more optimized.
To have no indexes on joins / where (used key = NULL on explain), this is the reason why your queries are slow. You should index them in such a way :
CREATE INDEX thread_forumid_index ON thread(forumid);
CREATE INDEX userthreadviews_forumid_index ON userthreadviews(forumid);
Documentation here
Try to index the table forumid if it is not indexed
Suggestions:
move the conditions from the WHERE clause to the JOIN clause
put the JOIN with the conditions before the other JOIN
make sure you have proper indexes and that they are being used in the query (create the ones you'll need... too much indexes can be as bad as too few)
Here is my suggestion for the query:
SELECT
`thread`.`title` AS 'r_title',
`thread`.`threadid` AS 'r_threadid',
`thread`.`forumid` AS 'r_forumid',
`thread`.`lastposter` AS 'r_lastposter',
`thread`.`lastposterid` AS 'r_lastposterid',
`forum`.`title` AS 'f_title',
`thread`.`replycount` AS 'r_replycount',
`thread`.`lastpost` AS 'r_lastpost',
`userthreadviews`.`replycount` AS 'u_replycount',
`userthreadviews`.`id` AS 'u_id',
`thread`.`postusername` AS 'r_postusername',
`thread`.`postuserid` AS 'r_postuserid'
FROM
`thread`
INNER JOIN (`forum`)
ON ((`thread`.`visible` = 1)
AND (`thread`.`lastpost` > $time)
AND (`thread`.`forumid` IN ($ids))
AND (`thread`.`forumid` = `forum`.`forumid`))
LEFT JOIN (`userthreadviews`)
ON ((`thread`.`threadid` = `userthreadviews`.`threadid`)
AND (`userthreadviews`.`userid` = $userid))
ORDER BY
`thread`.`lastpost` DESC
LIMIT
0, 30
These are good candidates to be indexed:
- `forum`.`forumid`
- `userthreadviews`.`threadid`
- `userthreadviews`.`userid`
- `thread`.`forumid`
- `thread`.`threadid`
- `thread`.`visible`
- `thread`.`lastpost`
It seems you already have lots of indexes... so, make sure you keep the ones you really need and remove the useless ones.

Categories