Any way to speed up this MySQL query? - mysql

The query below runs quickly when the "FROM" clause has only a single account number (a.account_nbr) and a single object code (a.fin_object_cd), but when I modify the query so that the FROM clause has a range of account numbers and a range of object codes, it takes a VERY long amount of time to return results. It goes from about a minute to run to 20 or more minutes.
The query does return the results I want, but I need to make it run more quickly.
What can I do? I am not sure if adding indexes to some columns would help, or if there's just a better way to write the query.
If you need more information about the tables, or what I'm trying to accomplish, please let me know.
select
a.account_nbr as "Account Number",
a.account_nm as "Account Name",
a.fin_object_cd as "Object Code",
a.fin_obj_cd_nm as "Object Code Name",
(select COALESCE(sum(fin_beg_bal_ln_amt),0) from kfsprd.gl_balance_t where account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and univ_fiscal_yr = "2015" and fin_balance_typ_cd != "CB") as "Beginning Balance",
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "D") as "Debits",
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "C" and trn_ldgr_entr_amt is not null) as "Credits",
(
(select COALESCE(sum(fin_beg_bal_ln_amt),0) from kfsprd.gl_balance_t where account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and univ_fiscal_yr = "2015" and fin_balance_typ_cd != "CB") +
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "D") -
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "C")
) as "Ending Balance"
from kfsprd.kf_f_transaction_dtl a where univ_fiscal_yr = "2015" and (univ_fiscal_prd_cd between "01" and "13" or
univ_fiscal_prd_cd = "BB") and a.account_nbr between "1014000" and "1014005" and a.fin_object_cd between "0000" and "9999" and a.fin_balance_typ_cd != "CB"
group by account_nbr, fin_object_cd
order by account_nbr, fin_object_cd;

Are you able to add fin_coa_cd to the where clause? If that has the same primary key as the original table (gl_entry_t), then the first 3 columns of that table would be univ_fiscal_yr, fin_coa_cd, and account_nbr. If you have a chart code to use, it may help Oracle use that index in the main query.

Related

Exporting issues to excel using openpyxl(django) (cant seems to work with fetchall())

def export_as_xls(self, request, queryset):
opts = self.model._meta
file_name = unidecode(opts.verbose_name)
sql_query = '''SELECT
COUNT(id) AS No_Of_Report,
vendor,
country_code,
SUM(new_code)*100/SUM(sent) AS 'failure_rate',
SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate'
FROM sms_statistics
WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
GROUP BY vendor, country_code
ORDER BY vendor DESC;'''
This is mysql query i used to call for the data in mysql schema
field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
wb = Workbook()
ws = wb.active
ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
with connection.cursor() as cursor:
cursor.execute(sql_query)
objects = list(cursor.fetchall())
for row in cursor.fetchall():
objects = list(row)
ws.append(objects)
print(ws.append(row))
ws = style_output_file(ws)
I think the issue is right here for not being able to export to excel. Im not be using the right method to export the file from action.py
response =
HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx'
wb.save(response)
return response
export_as_xls.short_description = "Reports of Past 30 days"
export_as_xls.acts_on_all = True
Blockquote
I believe this part is fine as i tried exporting an empty file and its working as expexted
def export_thirty_days(self, request, queryset):
opts = self.model._meta
file_name = "Sms Statistic Report"
sql_query = '''SELECT
COUNT(id) AS No_Of_Report,
vendor,
country_code,
SUM(new_code)*100/SUM(sent) AS 'failure_rate',
SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate'
FROM sms_statistics
WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
GROUP BY vendor, country_code
ORDER BY COUNT(id) DESC;'''
field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
wb = Workbook()
ws = wb.active
ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
with connection.cursor() as cursor:
cursor.execute(sql_query)
for row in cursor.fetchall():
l = list(row)
ws.append(l)
ws = style_output_file(ws)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx'
wb.save(response)
return response
export_thirty_days.short_description = "Reports of Past 30 days"
export_thirty_days.acts_on_all = True
This is the solutions i've found to make it work

Ignore null values in JSON dictionary - Python 3

I am starting to learn python and unable to figure this out (spent hours googling it).
Here's what I have:
response = urllib.request.urlopen(url)
data = json.loads(response.read())
table_data = []
for i in data['items']:
attendeeid = i['attendee id']
if 'reg: how hear ' in i:
howhear = i['reg: how hear ']
else:
howhear = 'No Data'
orderid = i['order_id']
totalprice = i['total price']
ordernumber = i['order_number']
email = i['email']
state = i['state']
country = i['country']
companyname = i['company name']
packagename = i['package name']
orderdate = i['order date']
pipedurl = i['piped url']
revenue = i['revenue']
lineitemquantity = i['line item quantity']
timebaseitemprice = i['time base item price']
table_data.append([attendeeid, howhear, orderid, totalprice, ordernumber, email, state, country, companyname, packagename, orderdate, pipedurl, revenue, lineitemquantity, timebaseitemprice])
However, I am finding that not all records have a value in the state field. How do I create an exception to address null state values?
thanks.

Check Roles in Webmatrix

I add two users and two roles in the webpages_UsersInRoles. I am trying to check to role of the user logging into the application.
Here is my code:
var UserId = WebSecurity.GetUserId(HttpContext.Current.User.Identity.Name);
var User = WebSecurity.CurrentUserName;
var db = Database.Open("ApplicationServices");
var stQueryString = "SELECT * FROM Table1 WHERE (UserId) = (UserId) ORDER BY SubmitDate DESC";
var data = db.Query(stQueryString, UserId);
var grid = new WebGrid(data, rowsPerPage:20);
var AdminQueryString = "SELECT * FROM Table1 ORDER BY SubmitDate DESC";
var AdminData = db.Query(AdminQueryString);
var AdminGrid = new WebGrid(AdminData, rowsPerPage:20);
<div id="Divgrid">
#if (Roles.IsUserInRole(UserId, "admin") && (AdminData.Any())){
#AdminGrid.GetHtml(
tableStyle: "grid",
headerStyle: "grid-header",
footerStyle: "grid-footer",
alternatingRowStyle: "grid-alternating-row",
selectedRowStyle: "grid-selected-row",
rowStyle: "grid-row-style",
columns: AdminGrid.Columns(
AdminGrid.Column(header:"", format:#View),
AdminGrid.Column(header:"", format:#Treatment),
AdminGrid.Column("Name", format:#<text>#item.ClientName</text>),
AdminGrid.Column("Date", format:#<text>#item.SubmitDate</text>))) }
else if (Roles.IsUserInRole(UserId, "user") && (data.Any())) {
#grid.GetHtml(
tableStyle: "grid",
headerStyle: "grid-header",
footerStyle: "grid-footer",
alternatingRowStyle: "grid-alternating-row",
selectedRowStyle: "grid-selected-row",
rowStyle: "grid-row-style",
columns: grid.Columns(
grid.Column(header:"", format:#View),
grid.Column(header:"", format:#Treatment),
grid.Column("Name", format:#<text>#item.ClientName</text>),
grid.Column("Date", format:#<text>#item.SubmitDate</text>))) }
</div>
Here is the error I am getting:
CS1502: The best overloaded method match for 'System.Web.Security.Roles.IsUserInRole(string, string)' has some invalid arguments
The IsUserInRole takes two parameters, the first is the User name, not the user ID. You may be able to use WebSecurity.CurrentUserName in place of where you have UserID.
One problem is the query needs to change to allow passing the username as a parameter:
var stQueryString = "SELECT * FROM Table1 WHERE UserId = #0 ORDER BY SubmitDate DESC";
The #0 indicates to take the parameter being passed in the Query.

Getting results from sql statement in dart, odbc

i got i hope simple question but i can't find answers in internet or i just still dont understand how to do that.
I want to execute sql statement and than get result and insert in my table, but i can't find proper function for that like there are in jdcb.
i found some examples but still dunno what to do.
code:
static void RegisterTable()
{
var hStmt = new SqlHandle();
sqlAllocHandle(SQL_HANDLE_STMT, MyConnect, hStmt);
sqlPrepare(hStmt, "SELECT R.[ID],U.[NAME],C.[CAR_NAME],R.[DESTINATION],R.[START_DATE],R.[END_DATE],R.[STATUS] FROM CAR_BOOKING.Registr_car R JOIN CAR_BOOKING.CARS C ON R.Car_id = C.id JOIN CAR_BOOKING.Users_table U ON U.id = R.user_id WHERE R.STATUS !=2;", SQL_NTS);
// Bind result set columns.
var id = new SqlIntBuffer();
var name = new SqlStringBuffer(70);
var carname = new SqlStringBuffer(70);
var destination = new SqlStringBuffer(70);
var startdate = new SqlDateBuffer(70);
var enddate = new SqlDateBuffer();
var status = new SqlIntBuffer(70);
sqlBindCol(hStmt, 1, id.ctype(), id.address(), id.length(),null);
sqlBindCol(hStmt, 2, name.ctype(), name.address(), name.length(),null);
sqlBindCol(hStmt, 3, carname.ctype(), carname.address(), carname.length(),null);
sqlBindCol(hStmt, 4, destination.ctype(), destination.address(), destination.length(),null);
sqlBindCol(hStmt, 5, startdate.ctype(), startdate.address(), startdate.length(),null);
sqlBindCol(hStmt, 6, enddate.ctype(), enddate.address(), enddate.length(),null);
sqlBindCol(hStmt, 6, status.ctype(), status.address(), status.length(),null);
sqlExecute(hStmt);
List<Object> result = sqlFetch(hStmt);
sqlFreeHandle(SQL_HANDLE_STMT, hStmt);
}
How to get this result to any container like List or print into rows ?

How toPerform a left join in linq pad -

How can I get linq pad to run my left join as show below?
var query =
from s in db.CDBLogsHeaders
.OrderByDescending(g => g.LogDateTime)
from sc in db.StyleColors
.Where(stylecolor => stylecolor.id == (int?)s.StyleColorID)
.DefaultIfEmpty()
from c in db.StyleHeaders
.Where(styleHeader => styleHeader.id == (int?)s.StyleHeaderID)
.DefaultIfEmpty()
select new
{
CDBLogsHeaderId = s.Id,
Merchandiser = c.Merchandiser,
Material = s.Material,
Season = s.Season,
LogsHeaderLogType = s.LogType,
PushFromTo = s.PushFromTo,
LinePlan = s.LinePlan,
QuikRefNumber = s.QuikRefNumber,
PLMOrigin = s.PLM_Origin,
SeasonOriginal = c.SeasonOriginal,
SeasonCurrent = c.SeasonCurrent,
StyleHeaderId = c.Id,
StyleCode = c.StyleCode,
StyleColorsColorCode = sc.ColorCode
};
query.Dump();
The sql that linq pad creates runs perfectly in Management Studio but linq-pad doesn't display any rows and gives this error
InvalidOperationException: The null value cannot be assigned to a
member with type System.Int32 which is a non-nullable value type.
How can I get linqpad to work so I can play with it?
In your anonymous type, make sure your ints are returning ints.
Change
StyleHeaderId = c.Id,
To
StyleHeaderId = (c.Id == null ? 0 : c.Id),