I am trying to figure out where laravel makes the session query, i have these queries when i just log in my app
/****************************** Database Queries ******************************/
0.79 | select * from `users` where `users`.`id` = ? limit 1 | 1
0.8 | select `roles`.*, `role_user`.`user_id` as `pivot_user_id`............
Queries: 2
Time: 1.59
/****************************** End Queries ***********************************/
I would like to eager load the first query with the second query, i use the second query in every pages of my website to get the user nav bar according with his role
if i do Auth::user()->roles()->id the second query would run.. but if i do User::with('Role')->find(1); for example i would get the same result as the first query plus+ the second.. am i right? how could i change this laravel behavior to runs trough all the pages.. i would save one query :)
hope i could explain myself clearly..
Related
I have 40 rows in my table, what i want is to get 20 row randomly, and after order these 5 list by id; these 2 in one query;
for example in range of id 1 to 40; i get from the random order 15 10 14 9 3;so in the final result i expect to get 3 9 10 14 15
i tried to use inRandomOrder and orderBy in same time
$query->inRandomOrder()->orderBy('id', 'asc')
but the inRandomOrder query always has the final execution of my query; and if i switch their position:
$query->orderBy('id', 'asc')->inRandomOrder()
the orderBy always has the final execution
2 solutions I guess, if you manipulate only dozens of rows, you can use Laravel collection to order by ID your query result.
Just use:
$result = $query->inRandomOrder()->get();
$resultSortedById = $result->sortBy('id');
Or using SQL query, you can use this query:
SELECT * FROM
(
SELECT * FROM table
ORDER BY RAND()
LIMIT 20
) result
ORDER BY id ASC
You can translate in Eloquent way, or as SQL directly.
I am not sure to understand your question btw: order these 5 list by id ? What do you mean exactly ?
I have an 'articles' table (Model: Article) where there is field named 'pageview'. Each Article has different number of pageview. I want to fetch those rows who holds top 10 pageviews. How can I fetch these data using eloquent?
You can first orderBy DESC the articles and then take out 10 records of the model. Try this!
$articles = Article::orderBy('pageview', 'DESC')->take(10)->get();
You can do this with the orderBy method which ordering data.
hence you need top 10 page views, you can do this.
orderBy('PageView','desc')
Now you have to take only 10 so use method take(). It actually means limit in MySQL. here's your code
Article::orderBy('PageView','desc')->take(10)->get();
Mysql query is now
select * from articles order by PageView desc limit 10
It will do the trick:
Article::orderBy('PageView','Desc')->take(10)->get();
Article::orderBy('PageView','DESC')->take(10)->get();
or (without column name)
Article::latest()->take(10)->get();
I am asking a very beginner level of question but I am always confused whenever I want to use aggregate function with Group by. Actually I am getting the right results but I am not pretty sure about how group by is working here. My requirement is to get the count of sent items which is based on MessageGroup columns.
MessageId SenderId MessageGroup Message
_____________________________________________________________________________
1 2 67217969-e03d-41ec-863e-659ca26e660f Hi
2 2 67217969-e03d-41ec-863e-659ca26e660f Hello
3 2 67217969-e03d-41ec-863e-659ca26e660f bye
4 1 c45dc414-9320-40a5-8f8f-9c960d6deffe TC
5 1 8486d16b-294b-45a5-8674-e7024e55f39b shutup
Actually I want to get the count for sent messages.here SenderId=2 has sent three messages to someone but I want to show a single count so I have used MessageGroup and I am doing Groupby and getting the count.
I have used Linq query::
return DB.tblMessage.Where(m => m.SenderId == 2 ).GroupBy(m => m.MessageGroup).Count();
This returns "1" which is correct and I want to show (1) in sent messages.
But if I try to query the above in SQL Server, it returns 3
Here is my SQL query:
select count(*)
from tblMessage
where SenderId = 2
group by MessageGroup
The Linq query is right As it returns me one as Microsoft says here
Actually I am confused with Group by. Please clear my point.
When you are using GroupBy, which ever columns present in groupBy Clause should be in Select Clause
select MessageGroup,count(MessageGroup)from tblMessage
where SenderId=2
group by MessageGroup
You want to include MessageGroup as part of the select, like this:
select MessageGroup, count(*)
from tblMessage
where SenderId=2
group by MessageGroup
I have two views in an ORACLE DB:
The view USERS with columns:
UserId | GroupId | Details
where there's 1 record for each User, total 40.000, and there c.a. 1-30 users in each Group.
and
The view SUMMARY with columns:
UserId | Summary
where there's 1 record for each User
The SUMMARY view is very complex but is still quite fast when i query it based on user.
That is:
select * from SUMMARY where UserId='some_user_id'
performs in 0,1 sec
The USERS view is pretty simple (it's a union all of USERS_TYPE1 and USERS_TYPE2 tables)
and a query of the type:
select * from USERS where GroupId='some_group_id'
performs in 0,02 sec
Now here's the catch,
when I do:
select * from USERS JOIN SUMMARY
ON USERS.UserId = SUMMARY.UserId
WHERE USERS.GroupId = 'some_group_id'
I get AWFUL performance of 90seconds - even though there are only 3 users in the group.
This should take only a fraction of a second if the user ID's are found first and then the SUMMARY table is queried with those user ID's.
Is there a way I can hint the DB to do that?
I have indexes based on UserId, GroupId and (GroupId,UserId) on all underlying tables.
((This is pretty hard to reproduce In a simple manner since the complex view has a lot of underlying tables. I have a couple of versions of the SUMMARY view. In some cases (depending on how SUMMARY is built) it stops being awful when i materialize the Users view, but in other cases it does not.))
You could try
SELECT *
FROM
(select * from USERS
WHERE USERS.GroupId = 'some_group_id') u,
summary
WHERE USERS.UserId = SUMMARY.UserId
Also specify exactly what columns you want, rather than select * (you obviously don't need both versions of the UserId)
It MAY be worth trying
SELECT *
FROM
(select users.*, rownum rn from USERS
WHERE USERS.GroupId = 'some_group_id') u,
summary
WHERE USERS.UserId = SUMMARY.UserId
which will force it to evaluate the inline view first.
Views on views...best to be avoided, or else handled very (very) carefully. Big performance problems, and it's a complicated subject, but here is some basic info:
http://www.dba-oracle.com/concepts/views.htm
http://www.dba-oracle.com/art_hints_views.htm
I'm doing some db querys using ActiveRecord and i need to paginate the results so i do a
$this->db->limit($pPagination['Start'], $pPagination['Length']);
but i don't get any results. Using $this->db->last_query(); it seems that CodeIgniter produces the following SQL
SELECT *
FROM (`viw_contacts`)
WHERE `user_id` = '1'
ORDER BY `contact_name` asc
LIMIT 0 OFFSET 15
which when i run it inside PHPMyAdmin also returns 0 rows.
But if i modify it an run :
SELECT *
FROM (`viw_contacts`)
WHERE `user_id` = '1'
ORDER BY `contact_name` asc
LIMIT 0, 15
Then i get the correct results. Any ideea why CodeIgniter generates this SQL and why it doesn't work?
I use CodeIgniter 1.7.3 and MySQL 5.1.41
Ok, found the issue.
It's
$this->db->limit($pPagination['Length'], $pPagination['Start']);
instead of
$this->db->limit($pPagination['Start'], $pPagination['Length']);
First param is the Length and second is the offset, not the other way around as i thought.