Eloquent prepares query but does not execute - laravel

I have a 'customer' table and I am trying to get a record using Laravel Eloquent using the customer's id:
Customer::where('customer_id', '=', $customer_id)->get();
However, when this gets executed I check the MySQL logs and I get:
Prepare select `kanji_name` from `customer` where `customer_id` = ?
Close stmt
Quit
As you can see, there is no 'Execute ...' log which should be present of the query was executed after preparing it.
I also tried:
Customer::find($customer_id);
with the same result.
Does anyone know the reason why?

can you do this
$cust = Customer::where('customer_id', '=', $customer_id)->get();
dd($cust);
Sorry, not enugh reps to comment. What's DD output?

It seems there is some form of caching that is happening either on the Eloquent side or MySQL side (possibly on MySQL side).
I restarted my MySQL server and found that on my first try, I get
Prepare select * from `customer` where `customer_id` = ?
Execute select * from `customer` where `customer_id` = '4058'
Close stmt
Quit
While on succeeding tries, I get:
Prepare select * from `customer` where `customer_id` = ?
Close stmt
Quit
only.

Related

Oracle not using index, Entity Framework & Devart DotConnect for oracle

The table in question has ~30mio records. Using Entity Framework I write a LINQ Query like this:
dbContext.MyTable.FirstOrDefault(t => t.Col3 == "BQJCRHHNABKAKU-KBQPJGBKSA-N");
Devart DotConnect for Oracle generates this:
SELECT
Extent1.COL1,
Extent1.COL2,
Extent1.COL3
FROM MY_TABLE Extent1
WHERE (Extent1.COL3 = :p__linq__0) OR ((Extent1.COL3 IS NULL) AND (:p__linq__0 IS NULL))
FETCH FIRST 1 ROWS ONLY
The query takes about four minutes, obviously a full table scan.
However, handcrafting this SQL:
SELECT
Extent1.COL1,
Extent1.COL2,
Extent1.COL3
FROM MY_TABLE Extent1
WHERE Extent1.COL3 = :p__linq__0
FETCH FIRST 1 ROWS ONLY
returns the expected match in 200ms.
Question: Why is it so? I would expect the query optimizer to note that the right part is false if the parameter is not null, so why doesn't the first query hit the index?
Please set UseCSharpNullComparisonBehavior=false explicitly:
var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.QueryOptions.UseCSharpNullComparisonBehavior = false;
If this doesn't help, send us a small test project with the corresponding DDL script so that we can investigate the issue.

Laravel executes 2 queries for a single eloquent create() method

I just noticed, that when I'm trying to execute a single UserLog::create($data_array) query.
Laravel executes 2 queries instead.
This isn't causing any problems, I'm just trying to understand why? (as I'm already telling it which columns to set data for in the $data_array)
Is there any way to reduce it to a single query?
Here are the 2 queries executed for UserLog::create($data_array):
1. select column_name as `column_name` from information_schema.columns where table_schema = 'database_name' and table_name = 'user_logs'
2. insert into `user_logs` (...) values (...)
Edit: I'm only doing a single create() in the page I'm experiencing this, and when I comment out the line, no queries are executed.

Update Oracle table using functions

I have the below queries and would like to update the corresponding data in the table that the queries return. Is it possible to utilize an update statement with an Oracle function? If so, how?
select *
from A
where translate(transnbr, '_0123456789', '_') is not null ;
select *
from A
where regexp_like(transnbr, '/*-+.') ;
Both queries return records as expected.
Would something along the lines the below work?
update a
set transnbr = translate(transnbr, '_0123456789', '_') ;
update a
set transnbr = regexp_like(transnbr, '/*-+.') ;
Thanks for any replies

Running "exists" queries in Laravel query builder

I'm using MySQL and have a table of 9 million rows and would like to quickly check if a record (id) exists or not.
Based on some research it seems the fastest way is the following sql:
SELECT EXISTS(SELECT 1 FROM table1 WHERE id = 100)
Source: Best way to test if a row exists in a MySQL table
How can I write this using Laravel's query builder?
Use selectOne method of the Connection class:
$resultObj = DB::selectOne('select exists(select 1 from your_table where id=some_id) as `exists`');
$resultObj->exists; // 0 / 1;
see here http://laravel.com/docs/4.2/queries
Scroll down to Exists Statements, you will get what you need
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('table1')
->whereRaw("id = '100'");
})
->get();
This is an old question that was already answered, but I'll post my opinion - maybe it'll help someone down the road.
As mysql documentation suggests, EXISTS will still execute provided subquery. Using EXISTS is helpful when you need to have it as a part of a bigger query. But if you just want to check from your Laravel app if record exists, Eloquent provides simpler way to do this:
DB::table('table_name')->where('field_name', 'value')->exists();
this will execute query like
select count(*) as aggregate from `table_name` where `field_name` = 'value' limit 1
// this is kinda the same as your subquery for EXISTS
and will evaluate the result and return a true/false depending if record exists.
For me this way is also cleaner then the accepted answer, because it's not using raw queries.
Update
In laravel 5 the same statement will now execute
select exists(select * from `table_name` where `field_name` = 'value')
Which is exactly, what was asked for.

Codeigniter select multiple rows?

How do I select multiple rows from SQL?
ie. $results->row(1,2,3,4,5,10)
Are you using ActiveRecord? If so, you can use the where_in() method when making your query. It's not something you do after the query is finished, as you seem to be doing in your example.
$this->db->where_in('id', array(1,2,3,4,5,10));
$query = $this->db->get('myTable');
// This produces the query SELECT * FROM `myTable` WHERE `id` IN (1,2,3,4,5,10)
See the this CodeIgniter docs section for more info on SELECT statement support.

Resources