I am working with multiple databases in single project. When I run simple raw query to get another database table count but it always return zero instead of actual counts. Even I have more than 1 million records in table.
I run raw query in following formats but result is zero
$dbconn = \DB::connection("archive_db");
$dbconn->table('activities_archived')->count()
$sql = "SELECT COUNT(*) as total FROM activities_archived";
$result = \DB::connection("archive_db")->select(\DB::raw($sql));
Event I have set the database connections strict option to false but still facing same issue.
Now I am totaly stuck that why this issue is coming
$someModel->setConnection('mysql2');
$something = $someModel->count();
return $something;
Related
I'm using EF Core but I'm not really an expert with it, especially when it comes to details like querying tables in a performant manner...
So what I try to do is simply get the max-value of one column from a table with filtered data.
What I have so far is this:
protected override void ReadExistingDBEntry()
{
using Model.ResultContext db = new();
// Filter Tabledata to the Rows relevant to us. the whole Table may contain 0 rows or millions of them
IQueryable<Measurement> dbMeasuringsExisting = db.Measurements
.Where(meas => meas.MeasuringInstanceGuid == Globals.MeasProgInstance.Guid
&& meas.MachineId == DBMatchingItem.Id);
if (dbMeasuringsExisting.Any())
{
// the max value we're interested in. Still dbMeasuringsExisting could contain millions of rows
iMaxMessID = dbMeasuringsExisting.Max(meas => meas.MessID);
}
}
The equivalent SQL to what I want would be something like this.
select max(MessID)
from Measurement
where MeasuringInstanceGuid = Globals.MeasProgInstance.Guid
and MachineId = DBMatchingItem.Id;
While the above code works (it returns the correct value), I think it has a performance issue when the database table is getting larger, because the max filtering is done at the client-side after all rows are transferred, or am I wrong here?
How to do it better? I want the database server to filter my data. Of course I don't want any SQL script ;-)
This can be addressed by typing the return as nullable so that you do not get a returned error and then applying a default value for the int. Alternatively, you can just assign it to a nullable int. Note, the assumption here of an integer return type of the ID. The same principal would apply to a Guid as well.
int MaxMessID = dbMeasuringsExisting.Max(p => (int?)p.MessID) ?? 0;
There is no need for the Any() statement as that causes an additional trip to the database which is not desirable in this case.
So i am trying to get distinct collections like this
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->get();
But when i do a count($data) I get 1600 but when i run
$data = DB::table('project_1_data')->distinct('Farmer_BankVerificationNumber_Farmer')->count();
I get 1440. This is weird as i only want collection with distinct field 'Farmer_BankVerificationNumber_Farmer'. How do i write the query correctly?
It's because you're asking your query to count the distinct values in the wrong place.
You need to tell your count parameter what field you would like to count on. So you'll basically ask the query to get the database information, separate the distinct values and then count how many of a specific field are distinct.
Your finished query should look like this:
$data = DB::table('project_1_data')
->distinct()
->count('Farmer_BankVerificationNumber_Farmer');
I have the code which is working perfectly fine.
$amenityCategoryMapping1 = AmenityCategoryMapping::where('property_id', $property->id)
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping2 = AmenityCategoryMapping::where('company_id', $property->company_id)
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping3 = AmenityCategoryMapping::whereNull('property_id')
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping = array_merge($amenityCategoryMapping3, $amenityCategoryMapping2, $amenityCategoryMapping1 );
However, currently I am executing 3 different query to extract data from the same table just using different parameters. Is there any way to minimize this query requests and still receiving the same results?
Currently, while plucking if two array has the same key then I am replacing amenityCategoryMapping3 by amenityCategoryMapping2 and that by amenityCategoryMapping1. That is more priority should be given to amenityCategoryMapping1
Try using orWhere function
$amenityCategoryMapping1 = AmenityCategoryMapping::where('property_id', $property->id)
->orWhere('company_id', $property->company_id)
->orWhereNull('property_id')
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
I am facing a strange problem trying to execute a simple request using laravel-doctrine/orm v1.2.5 on Lumen v5.2.7. with an Oracle database.
When I create a query with the query builder containing more than 2 parameters, I get no results, even though I am expecting a result set.
$conn = $this->getEntityManager()->getConnection();
$queryBuilder = $conn->createQueryBuilder();
$queryBuilder->select('*')
->from('SYNCHS')
->where('SYNCHS.CLIENT_ID = :clientId')
->andWhere('SYNCHS.TOP_CLIENT_ID = :topClientId')
->andWhere('SYNCHS.ID = :synchId');
$queryBuilder->setParameter('clientId', $clientId)
->setParameter('topClientId', $topClientId)
->setParameter('synchId', $synchId);
echo $queryBuilder->getSQL();
var_dump($queryBuilder->getParameters());
$stmt = $queryBuilder->execute();
var_dump($stmt);
$results = $stmt->fetchAll();
dd($results);
If I comment any one of the where/addwhere clause, I end up with the expected result set, but if I have more than 2 parameters bound in the query, it returns nothing (of course it should return something with the passed parameters).
For every scenari:
I can see all my parameters are bound in the query builder var_dump($queryBuilder->getParameters());
Copy paste of the echo $queryBuilder->getSQL(); in Toad (oracle) setting the parameters with the values displayed by the var_dump($queryBuilder->getParameters()); give me 1 row (expected result). So I am sure that my query is correct.
I've looked for hours on google but no relevant result appeared for this problem. Thanks
I want to do two things in one query: use the limit() function, but also get the total number of records as a record count for the query, as if the limit wasn't used. So, if there are 21 total records that meet the conditions like '%Gregory%', I would like that value returned, even though I'm using a limit(10,0).
Here's my code:
$data['recordcount'] = $this->db->count_all_results('assets');
$this->db->limit(10, 0);
if(isset($data['order'])){
$query = $this->db->select('*')->from('assets')->or_like($array)->order_by($column, $data['order'])->get();
}
The problem is that $data['recordcount'] in the first line returns all records. I want it to return all records that meet the query condition in the 4th line, but without the limit found in the 2nd line.
Normally count_all_results() clears the select statement when run. But by setting the second parameter to FALSE the statement is retained.
I think this will work for you.
$this->db->select('*')->from('assets')->or_like($array);
$data['recordcount'] = $this->db->count_all_results('assets', FALSE);
$this->db->limit(10, 0);
if(isset($data['order']))
{
$this->db->order_by($column, $data['order']);
}
$query = $this->db->get();
You can achieve SQL_CALC_FOUND_ROWS before the asterisk (*) like this:
$this->db->select('SQL_CALC_FOUND_ROWS *')->from('assets')..etc
This will calculate the number of rows that would be returned without the LIMIT condition.
Immediately after that you can select the FOUND_ROWS() like this:
$this->db->select('FOUND_ROWS()')->get();
This will return you the number you are looking for.
For more information refer to this page
enter link description here