Laravel Excel time out when create csv file - laravel-excel

I am trying to create a csv file with 100 000 records in ,but it is timing out. If i run 10 000 records it works fine.
$users1 = Aapplicant::select(DB::raw('applicant.AppID as Application_ID,
tapplicant.Email,
tapplicant.AppDate as Application_Date,
tapplicant.AppIPAddress as IP_Address,
tapplicant.AppAffID as Affiliate_ID,
toutcome.Status as Application_Status,'))
->join('outcome', 'outcome.AppID', '=', 'applicant.AppID')
->where('AppDate', '>=', \Carbon\Carbon::now()->startOfMonth())
->orderBy('tapplicant.AppID', 'DESC')
->get();
Excel::create('tapplicant', function($excel) use($users1) {
$excel->sheet('Sheet 1', function($sheet) use($users1) {
$sheet->fromArray($users1);
});
})->store('csv');

Related

Laravel Controller: translating an SQL SELECT DISTINCT statement inside a function

Table List:
Here is the SQL as mentioned in the title:
SELECT DISTINCT ON (hbg.group_id)
hbg.group_id,
hbg.id AS id,
hbg.group_name,
hbg.group_description,
hbg.group_type_id,
hbgt.group_type_name,
hbg.category_id,
hbg.effective_start_datetime,
hbg.created_by,
hbg.created_datetime,
hbg.archived_by,
hbg.archived_datetime
FROM hms_bbr_group hbg
LEFT JOIN hms_bbr_group_type hbgt
ON hbg.group_type_id = hbgt.group_type_id
ORDER BY
hbg.group_id,
hbg.id DESC;
test in pgAdmin: (only selecting distinct id)
I am trying to translate this inside a function in my Laravel Controller
Function:
public function fetchgroup(){
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group_type.group_type_id')
->distinct('group_id')
->orderBy('group_id', 'ASC', 'id', 'DESC')->get();
return response()->json([
'all_groups'=>$all_groups,
]);
}
The code above works differently though, the screenshot below is the output. The rows shown are different:
I tried changing up the orderBy but the rows shown are still not the correct id (the highest value id on every group_id)
any help would be appreciated thanks
UPDATE:
tried tweaking the function abit but the output is exactly the same as the screenshot:
$all_groups = HmsBbrGroup::leftJoin('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group_type.group_type_id')
->distinct('hms_bbr_group.group_id')
->orderBy('group_id', 'ASC', 'id', 'DESC')->get();
UPDATE 2
I tried changing up the distinct in the function but the code below does not work as intended as well
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group_type.group_type_id')
->selectRaw('DISTINCT ON (group_id),
hms_bbr_group.group_id,
hms_bbr_group.id AS id,
hms_bbr_group.group_name,
hms_bbr_group.group_description,
hms_bbr_group.group_type_id,
hms_bbr_group_type.group_type_name,
hms_bbr_group.category_id,
hms_bbr_group.effective_start_datetime,
hms_bbr_group.created_by,
hms_bbr_group.created_datetime,
hms_bbr_group.archived_by,
hms_bbr_group.archived_datetime')
->orderBy('group_id', 'ASC', 'id', 'DESC')->get();

Does it is a good practice to store some numeric values in wp_postmeta table and fetch it using sql query? (Wordpress, Database)

Well i have made a script in which when ever the user watch a complete video the user data (post_id,user_id) will get store in wp_postmeta table. These values are just numeric does it will affect my website performance if the rows in wp_postmeta increases?
$chstr =$wpdb->get_results( "select meta_value,meta_key from $wpdb->postmeta where meta_key = $new_post_id AND meta_value= $cur_user_id" );
if(count($chstr) > 0){
exit;
}else{
$wpdb->insert(
'wp_postmeta',
array(
'post_id'=> $new_post_id,
'meta_key' => $new_post_id,
'meta_value' => $cur_user_id
),
array(
'%s'
)
);
die();
return true;
}
It affects some level.
But fetch post meta with Wordpress SQL Query it is the best way to get data: https://codex.wordpress.org/Class_Reference/wpdb
you can use this function to save current user id into database.
function save_post_meta_userid( $post_id ) {
$curr_userid = get_current_user_id();
update_post_meta( $post_id, 'post_currentuserid', $curr_userid );
}

using sum with relation Laravel Eloquent ORM

i have Model Material which has
protected $with = ['costPrices'];
public function costPrices(){
return $this->hasMany(CostPrice::class);
}
the table of cost_prices has multiple quantity
//create_cost_prices_table
Schema::create('cost_prices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('material_id');
$table->unsignedBigInteger('supplier_id');
$table->integer('quantity');
$table->timestamps();
});
i want to get (insufficient materials ) in another way select all materials form materials table where sum of all quantities of cost_prices table < 3 and get me the resulted materials not the count
You have 2 options:
You could fix your option with whereHas() by adding select and groupBy:
$materials = Material
::whereHas('costPrices', function($q) {
$q
->select('meterial_id')
->groupBy('material_id')
->havingRaw('SUM(quantity) > 4');
})
->get();
Or use subquery:
For laravel 6.x:
$materials = Material
::addSelect([
'cost_prices_sum' => CostPrice::selectRaw('sum(quantity)')->whereColumn('material_id', 'material.id')
])
->having('cost_prices_sum', '>', 4)
->get();
For laravel 5.x:
$materials = Material
::selectRaw('carts.*, (select sum(quantity) from cost_prices where cost_prices.matrerial_id = meterials.id) as cost_prices_sum')
->having('cost_prices_sum', '>', 4)
->get();
UPDATE: option with whereHas() is faster on a large number of rows.

ZF2: Is there a more efficient way to do this Zend\Db Update query?

Here's some ZF1 code for an update query:
$this->getAdapter()->update(
'users', $data, $this->getAdapter()->quoteInto('node_id = ?', $user->nodeId)
);
Here's the same query with ZF2:
$param = $this->getAdapter()->platform->quoteValue($user->nodeId);
$sqlOj = new Sql($this->getAdapter());
$update = $sqlOj->update('users')->set($data)->where('node_id = ' . $param);
$updateString = $sqlOj->getSqlStringForSqlObject($update);
$this->getAdapter()->query($updateString, Adapter::QUERY_MODE_EXECUTE);
As you can see, one line of ZF1 code has become 5 lines of ZF2 code, (actually without the fluent interface it would be 7 lines...)
Am I missing something? Or is ZF2's DB component just more verbose that ZF1?
BTW, I have found the same scenario with select and insert queries too...
I managed to limit it to 3 lines.
use \Zend\Db\Sql\Sql;
$sql = new Sql ($adapter);
$update = $sql->update ('users')->set ($data)->where (['id = ?' => 1]);
$adapter->query ($sql->getSqlStringForSqlObject ($update), $db::QUERY_MODE_EXECUTE);
The problem is they didn't expect you to run your updates like that. Instead, you are expected to use a table gateway.
This way it becomes one line again:
$this->tableGateway->update($data, array('id' => $id));

Raven query returns 0 results for collection contains

I have a basic schema
Post {
Labels: [
{ Text: "Mine" }
{ Text: "Incomplete" }
]
}
And I am querying raven, to ask for all posts with BOTH "Mine" and "Incomplete" labels.
queryable.Where(candidate => candidate.Labels.Any(label => label.Text == "Mine"))
.Where(candidate => candidate.Labels.Any(label => label.Text == "Incomplete"));
This results in a raven query (from Raven server console)
Query: (Labels,Text:Incomplete) AND (Labels,Text:Mine)
Time: 3 ms
Index: Temp/XWrlnFBeq8ENRd2SCCVqUQ==
Results: 0 returned out of 0 total.
Why is this? If I query for JUST containing "Incomplete", I get 1 result.
If I query for JUST containing "Mine", I get the same result - so WHY where I query for them both, I get 0 results?
EDIT:
Ok - so I got a little further. The 'automatically generated index' looks like this
from doc in docs.FeedAnnouncements
from docLabelsItem in ((IEnumerable<dynamic>)doc.Labels).DefaultIfEmpty()
select new { CreationDate = doc.CreationDate, Labels_Text = docLabelsItem.Text }
So, I THINK the query was basically testing the SAME label for 2 different values. Bad.
I changed it to this:
from doc in docs.FeedAnnouncements
from docLabelsItem1 in ((IEnumerable<dynamic>)doc.Labels).DefaultIfEmpty()
from docLabelsItem2 in ((IEnumerable<dynamic>)doc.Labels).DefaultIfEmpty()
select new { CreationDate = doc.CreationDate, Labels1_Text = docLabelsItem1.Text, Labels2_Text = docLabelsItem2.Text }
Now my query (in Raven Studio) Labels1_Text:Mine AND Labels2_Text:Incomplete WORKS!
But, how do I address these phantom fields (Labels1_Text and Labels2_Text) when querying from Linq?
Adam,
You got the reason right. The default index would generate 2 index entries, and your query is executing on a single index entry.
What you want is to either use intersection, or create your own index like this:
from doc in docs.FeedAnnouncements
select new { Labels_Text = doc.Labels.Select(x=>x.Text)}
And that would give you all the label's text in a single index entry, which you can execute a query on.