MySQL Restraint Not Being Passed Down Through Views - mysql

I am working with a system that has multiple views chained upon each other. For some reason, there is a problematic view which isn't passing the restraint down through the chain (despite them having a key in common). For example, here is the visual explain statement. Visual Explain of SQL View
As you can see, the main view is based upon two other views (which, in turn, are based off another set of views). The problem view in this example is ResourcePointsAndCategories (on the right-hand side bottom). In this query, I am restraining results by a WHERE clause based on a column called HostID. HostID is in my supporting view; however, the key is not being passed down, and therefore, the view is loading 23,000 rows, instead of the 3 I want.
Any help to explain or correct this issue would be greatly appreciated. Thanks!
Edit: Sorry, should have included the code from the start:
Here is for the main view:
VIEW `test`.`resourcepointswithlookupsandcategorycountandportalpagescount` AS
SELECT
`resourcepointswithlookups`.`URL` AS `URL`,
`resourcepointswithlookups`.`Format` AS `Format`,
`resourcepointswithlookups`.`Host` AS `Host`,
`resourcepointswithlookups`.`ResourceID` AS `ResourceID`,
`resourcepointswithlookups`.`HostID` AS `HostID`,
`resourcepointswithlookups`.`PermitID` AS `PermitID`,
`resourcepointswithlookups`.`ResourceTitle` AS `ResourceTitle`,
`resourcepointswithlookups`.`FormatID` AS `FormatID`,
`resourcepointswithlookups`.`TypeID` AS `TypeID`,
`resourcepointswithlookups`.`Notes` AS `Notes`,
`resourcepointswithlookups`.`Description` AS `Description`,
`resourcepointswithlookups`.`ReferenceFirstName` AS `ReferenceFirstName`,
`resourcepointswithlookups`.`ReferenceLastName` AS `ReferenceLastName`,
`resourcepointswithlookups`.`ReferenceEmail` AS `ReferenceEmail`,
`resourcepointswithlookups`.`ReferencePermission` AS `ReferencePermission`,
`resourcepointswithlookups`.`ReferenceComment` AS `ReferenceComment`,
`resourcepointswithlookups`.`CreateDate` AS `CreateDate`,
`resourcepointswithlookups`.`CreateBy` AS `CreateBy`,
`resourcepointswithlookups`.`LastEditDate` AS `LastEditDate`,
`resourcepointswithlookups`.`LastEditBy` AS `LastEditBy`,
`resourcepointswithlookups`.`LastReview` AS `LastReview`,
`resourcepointswithlookups`.`LastReviewBy` AS `LastReviewBy`,
`resourcepointswithlookups`.`Type` AS `Type`,
`resourcepointswithlookups`.`LibraryURL` AS `LibraryURL`,
`resourcepointswithlookups`.`TypeCollection` AS `TypeCollection`,
`resourcepointsandcategoriescount`.`CategoryCount` AS `CategoryCount`,
`portalpagecountwithresourcepointsandportaltitle`.`PortalPageCount` AS `PortalPageCount`,
`resourcepointswithlookups`.`NormFileStatus` AS `NormFileStatus`,
`resourcepointswithlookups`.`NormFileStatusDate` AS `NormFileStatusDate`,
`resourcepointswithlookups`.`FileSystemStatus` AS `FileSystemStatus`,
`resourcepointswithlookups`.`FileSystemStatusDate` AS `FileSystemStatusDate`,
`resourcepointswithlookups`.`LanguageName` AS `LanguageName`,
`resourcepointswithlookups`.`LanguageCode` AS `LanguageCode`
FROM
((`test`.`resourcepointswithlookups`
LEFT JOIN `test`.`portalpagecountwithresourcepointsandportaltitle` ON ((`resourcepointswithlookups`.`ResourceID` = `portalpagecountwithresourcepointsandportaltitle`.`ResourcePointID`)))
LEFT JOIN `test`.`resourcepointsandcategoriescount` ON ((`resourcepointswithlookups`.`ResourceID` = `resourcepointsandcategoriescount`.`ResourceID`)))
And here's the code for the problem view:
VIEW `test`.`resourcepointsandcategoriescount` AS
SELECT
`resourcepointsandcategories`.`ResourceID` AS `ResourceID`,
`resourcepointsandcategories`.`HostID` AS `HostID`,
COUNT(`resourcepointsandcategories`.`CategoryID`) AS `CategoryCount`
FROM
`test`.`resourcepointsandcategories`
GROUP BY `resourcepointsandcategories`.`ResourceID`
And, ultimately, the view the problem view is based off of (which runs fine):
VIEW `test`.`resourcepointsandcategories` AS
SELECT
`test`.`resourcepoints`.`ResourceID` AS `ResourceID`,
`test`.`resourcepoints`.`HostID` AS `HostID`,
`test`.`lkupegcategories`.`Category` AS `Category`,
`test`.`resourcepointcategories`.`CategoryID` AS `CategoryID`
FROM
((`test`.`resourcepointcategories`
JOIN `test`.`resourcepoints` ON ((`test`.`resourcepointcategories`.`ResourceID` = `test`.`resourcepoints`.`ResourceID`)))
LEFT JOIN `test`.`lkupegcategories` ON ((`test`.`lkupegcategories`.`eGCategoryID` = `test`.`resourcepointcategories`.`CategoryID`)))
The query I am calling is:
SELECT * FROM test.resourcepointswithlookupsandcategorycountandportalpagescount WHERE HostID = 4532;

If I read your views correctly, you are filtering the first (big) view on HostID = 4532, but not filtering the other two views on it.
Perhaps the ON clauses need to include AND res....HostID = res...HostID.

Related

How to get rid of "the expression you entered exceeds the1,024-character limit for the query design

How to get rid of "the expression you entered exceeds the 1,024-character limit for the query design? As I would need to extract the huge data based on the query in the access below.
Please see my query below. Any help is appreciated.
SELECT dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) AS STARTDATE
FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING, Input_BP
WHERE (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE)="BP1B") AND ((DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])) Between #4/1/2021# And #4/30/2021#))
GROUP BY dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])
HAVING (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140218" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140226" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140952" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300310001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300313005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300352052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300469013" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300473072" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300482024" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582096" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582120" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300602001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300629020" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300654028" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300670016" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300761021" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300774099" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786051" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786069" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786085" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300843142" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300894517" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300897973" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300897981" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300964005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992725" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992733" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992741" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001294" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001302" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001310" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001328" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003407" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003464" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003472" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301010147" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301010154" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301030434" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301030558" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035045" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035060" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035144" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035151" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035169" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035276" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035284" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035300" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035318" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035326" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035334" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301036027" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301040052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301047008" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049269" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049293" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049301" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049319" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049343" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301064003" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301128006" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301176898" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301218849" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301344538"));
Thanks
Using an alias for the table name and replacing OR col = val1 OR col = val1 OR col2 = val ... with col in (val1, val2, val3, ...) will save you a lot of characters. It will also make the code a lot easier to understand. I also removed the reference to Input_BP, which did not seem to be contributing anything except the possibility of a ton of duplicate results.
SELECT T.Year,
T.Month,
T.Day,
T.PERIOD,
T.CONFIGURATIONNAME,
T.CONNECTIONMEMBERNAME,
T.VALUE,
T.MSSLID,
T.PRICINGRUNGROUPCODE,
DateValue([T]![STARTDATE]) AS STARTDATE
FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING As T
WHERE (((T.PRICINGRUNGROUPCODE)="BP1B")
AND ((DateValue([T]![STARTDATE])) Between #4/1/2021# And #4/30/2021#))
GROUP BY T.Year,
T.Month,
T.Day,
T.PERIOD,
T.CONFIGURATIONNAME,
T.CONNECTIONMEMBERNAME,
T.VALUE,
T.MSSLID,
T.PRICINGRUNGROUPCODE,
DateValue([T]![STARTDATE])
HAVING (T.MSSLID IN ("9300140218", ...));
If you want to be able to access the query design builder, I recommend copying and pasting maybe half of the repeated fields towards the bottom to a note/word file so you can delete some of the fields in SQL? And I'm not sure if this is for a report or a form, but try to use VBA to reduce the query; or maybe use the function Right() for the field MSSLID so you don't have to write the first "9300"?
(This is just a reduced version of your code; I'm not sure if you're making a report or form or if you just wanted to make a query)
SELECT dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) AS STARTDATE FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING, Input_BP WHERE (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE)="BP1B") AND ((DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])) Between #4/1/2021# And #4/30/2021#)) GROUP BY dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) HAVING (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140218" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140226" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140952" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300310001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300313005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300352052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300469013" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300473072" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300482024" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582096" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582120" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300602001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300629020" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300654028" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300670016" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300761021" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300774099" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786051"));
Let me know if this helps!

Django: ORM/SQL query speed significantly decreased after adding additional BooleanField or (SQL tinyint) to Django Filter

Using MySQL Latest Django:
I have a vaguely complex Django query that works quite quickly--until I add an additional "AND" with a Boolean Field--
See Below:
queriedForms = queryFormtype.form_set.filter(is_public=True)
newQuery = queriedForms.filter(formrecordattributevalue__record_value__icontains=term['TVAL'], formrecordattributevalue__record_attribute_type__pk=rtypePK)
newQuery = newQuery.filter(flagged_for_deletion=False)
logger.info(newQuery.query)
term['count'] = newQuery.count()
If I either remove the initial "is_public=True" or the final "flagged_for_deletion=False)--it works incredibly fast. If I use both as filters, it increases the time for the count() function by something like 2000%
The different QuerySet.query outputs are below:
SELECT `maqluengine_form`.`id`, `maqluengine_form`.`form_name`, `maqluengine_form`.`form_number`, `maqluengine_form`.`form_geojson_string`, `maqluengine_form`.`hierarchy_parent_id`, `maqluengine_form`.`is_public`, `maqluengine_form`.`project_id`, `maqluengine_form`.`date_created`, `maqluengine_form`.`created_by_id`, `maqluengine_form`.`date_last_modified`, `maqluengine_form`.`modified_by_id`, `maqluengine_form`.`sort_index`, `maqluengine_form`.`form_type_id`, `maqluengine_form`.`flagged_for_deletion` FROM `maqluengine_form` INNER JOIN `maqluengine_formrecordattributevalue` ON (`maqluengine_form`.`id` = `maqluengine_formrecordattributevalue`.`form_parent_id`) WHERE (`maqluengine_form`.`form_type_id` = 319 AND `maqluengine_form`.`is_public` = True AND `maqluengine_formrecordattributevalue`.`record_value` LIKE %seal% AND `maqluengine_formrecordattributevalue`.`record_attribute_type_id` = 18510 AND `maqluengine_form`.`flagged_for_deletion` = False)
SELECT `maqluengine_form`.`id`, `maqluengine_form`.`form_name`, `maqluengine_form`.`form_number`, `maqluengine_form`.`form_geojson_string`, `maqluengine_form`.`hierarchy_parent_id`, `maqluengine_form`.`is_public`, `maqluengine_form`.`project_id`, `maqluengine_form`.`date_created`, `maqluengine_form`.`created_by_id`, `maqluengine_form`.`date_last_modified`, `maqluengine_form`.`modified_by_id`, `maqluengine_form`.`sort_index`, `maqluengine_form`.`form_type_id`, `maqluengine_form`.`flagged_for_deletion` FROM `maqluengine_form` INNER JOIN `maqluengine_formrecordattributevalue` ON (`maqluengine_form`.`id` = `maqluengine_formrecordattributevalue`.`form_parent_id`) WHERE (`maqluengine_form`.`form_type_id` = 319 AND `maqluengine_form`.`is_public` = True AND `maqluengine_formrecordattributevalue`.`record_value` LIKE %seal% AND `maqluengine_formrecordattributevalue`.`record_attribute_type_id` = 18510)
The first takes about 20/30 seconds to perform the count(), while the second with only 1 of the two BooleanField's takes less than a second to perform the count()
=======================================
EDIT=======================
Apologies: since the question isn't obvious enough--why is adding an additional AND with a BooleanField increasing the query time by +2000%? Is anyone able to assist in figuring out WHY that's occurring. Thanks.
EDIT=========================
Also discovered that using a exclude(is_public=False) rather than filter(is_public=True) has the same effect as the solution below. Does anyone happen to know why an exclude() works fine--whereas the filter() does not?
==============================
Solution I came up with after a night's rest:
--I keep the query as is(I need it for later because it continues getting chain filtered)
--I need the count() from this stage--which is taking substantially longer than it should with the additional BooleanField AND
--I take a temporary values list to perform a len() on instead:
queriedForms = queryFormtype.form_set.all()
newQuery = queriedForms.filter(formrecordattributevalue__record_value__icontains=term['TVAL'], formrecordattributevalue__record_attribute_type__pk=rtypePK)
newQuery = newQuery.filter(flagged_for_deletion=False)
tempQuery = newQuery.values_list('is_public',flat=True)
finalQuery = [entry for entry in tempQuery if entry != 'False'] #Remove any indices that contain "False"
term['count'] = len(finalQuery)
The following counts that use chained filters after use the same technique--it's significantly faster--if not as fast as removing one of the Booleans from the filters.

stddev_samp changes value when created as a view

Why does when I put my select query as it is, it works well? so here is my query
SELECT
AVG(total_score) AS AVGtotal_Score,
STDDEV_SAMP(total_score) AS STDDEV_SAMPtotal_score,
AVG(Total_Answer_Time) AS AVGTotal_Answer_Time,
STDDEV_SAMP(Total_Answer_Time) AS STDDEV_SAMPTotal_Answer_Time,
AVG(Total_Hint) AS AVGTotal_Hint,
STDDEV_SAMP(Total_Hint) AS STDDEV_SAMPTotal_Hint,
AVG(Total_Hint_Time) AS AVGTotal_Hint_Time,
STDDEV_SAMP(Total_Hint_Time) AS STDDEV_SAMPTotal_Hint_Time
FROM
average_pivot
and here is the output:
z values image
and when I try to put it in a view like this:
create view ave_temp_z_table as (SELECT
AVG(total_score) AS AVGtotal_Score,
STDDEV_SAMP(total_score) AS STDDEV_SAMPtotal_score,
AVG(Total_Answer_Time) AS AVGTotal_Answer_Time,
STDDEV_SAMP(Total_Answer_Time) AS STDDEV_SAMPTotal_Answer_Time,
AVG(Total_Hint) AS AVGTotal_Hint,
STDDEV_SAMP(Total_Hint) AS STDDEV_SAMPTotal_Hint,
AVG(Total_Hint_Time) AS AVGTotal_Hint_Time,
STDDEV_SAMP(Total_Hint_Time) AS STDDEV_SAMPTotal_Hint_Time
FROM
average_pivot);
the output of the stddev_samp changes.. The output of the view is this
z values as a view

MySQL 'multiple rows' from two joins challenge

I have the following MySQL query joining two additional tables..
SELECT plants.plant_id,
plants.scientific_name,
plants_features.features,
plants_features.features_id
FROM plants_features,
plants2features,
plants
WHERE plants_features.features_id = plants2features.features_id
AND plants2features.plant_id = plants.plant_id
and I was happy with the output.
plant_id,scientific_name,features,features_id
3,"Actaea matsumurae 'White Pearl' (Bugbane)","colourful fruit",6
11,"Heuchera 'Beauty Colour' (Coral bells)","salt resistant/coastal",15
18,"Phyllostachys nigra (Black bamboo)","colourful bark",5
26,"Carex morrowii 'Silver Sceptre' (Japanese sedge)","drought tolerant",18
27,"Heuchera 'Obsidian' (Coral bells)","salt resistant/coastal",15
29,"Dianella tasmanica 'Tas Red' (Flax lily)","drought tolerant",18
38,"Stipa tenuissima (Mexican feather grass)","attractive seed-heads",2
38,"Stipa tenuissima (Mexican feather grass)","invasive/self seed/suckering",13
You can see from the plant_id '38' (bottom two rows) that it's outputting more than one row per record.
My question is guys, can you please advise what exactly the new MySQL query needs to be to ensure multiple rows are in one record by 'plant_id'?
Thanks in advance,
Richard.
You need grouping and aggregation functions. You can use GROUP_CONCAT to get comma separated list:
SELECT plants.plant_id,
plants.scientific_name,
GROUP_CONCAT(plants_features.features) AS features,
GROUP_CONCAT(plants_features.features_id) AS feature_ids
FROM plants_features
JOIN plants2features
ON plants_features.features_id = plants2features.features_id
JOIN plants
ON plants2features.plant_id = plants.plant_id
GROUP BY plants.plant_id, plants.scientific_name;
Also use JOIN syntax instead of comma and where.

MySQL - Perl: How to get array of zip codes within submitted "x" miles of submitted "zipcode" in Perl example

I have found many calculations here and some php examples and most are just over my head.
I found this example:
SELECT b.zip_code, b.state,
(3956 * (2 * ASIN(SQRT(
POWER(SIN(((a.lat-b.lat)*0.017453293)/2),2) +
COS(a.lat*0.017453293) *
COS(b.lat*0.017453293) *
POWER(SIN(((a.lng-b.lng)*0.017453293)/2),2))))) AS distance
FROM zips a, zips b
WHERE
a.zip_code = '90210' ## I would use the users submitted value
GROUP BY distance
having distance <= 5; ## I would use the users submitted value
But, I am having trouble understanding how to implement the query with my database.
It looks like that query has all I need.
However, I cannot even find/understand what b.zip_code actually is! (whats the b. and zips a, zips b?)
I also do not need the state in the query.
My mySQL db structure is like this:
ZIP | LAT | LONG
33416 | 26.6654 | -80.0929
I wrote this in attempt to return some kind of results (not based on above query) but, it only kicks out one zip code.
## Just for a test BUT, in reality I desire to SELECT a zip code WHERE ZIP = the users submitted zip code
## not by a submitted lat lon. I left off the $connect var, assume it's there.
my $set1 = (26.6654 - 0.20);
my $set2 = (26.6654 + 0.20);
my $set3 = (-80.0929 - 0.143);
my $set4 = (-80.0929 + 0.143);
my $test123 = $connect->prepare(qq{SELECT `ZIP` FROM `POSTAL`
WHERE `LAT` >= ? AND `LAT` <= ?
AND `LONG` >= ? AND `LONG` <= ?}) or die "$DBI::errstr";
$test123->execute("$set1","$set2","$set3","$set4") or die "$DBI::errstr";
my $cntr;
while(#zip = $test123->fetchrow_array()) {
print qq~$zip[$cntr]~;
push(#zips,$zip[$cntr]);
$cntr++;
}
As you can see, I am quite the novice so, I need some hand holding here with verbose explanation.
So, in Perl, how can I push zip codes into an array from a USER SUBMITTED ZIP CODE and user submitted DISTANCE in miles. Can be a square instead of a circle, not really that critical of a feature. Faster is better.
I'll tackle the small but crucial part of the question:
However, I cannot even find/understand what b.zip_code actually is! (whats the "b." and "zips a, zips b"?)
Basically, the query joins two tables. BUT, both tables being joined are in fact the same table - "zips" (in other words, it joins "zips" table to itself"). Of course, since the rest of the query needs to understand when you are referring to the first copy of the "zips" table and when to the second copy of the "zips" table, you are giving a table alias to each copy - to wit, "a" and "b"'.
So, "b.xxx" means "column xxx from table zips, from the SECOND instance of that table being joined".
I don't see what's wrong with your first query. You have latitude and longitude in your database (if I'm understanding, you're comparing a single entry to all others). You don't need to submit or return the state that's just part of the example. Make the first query work like this:
my $query = "SELECT b.zip_code,
(3956 * (2 * ASIN(SQRT(
POWER(SIN(((a.lat-b.lat)*0.017453293)/2),2) +
COS(a.lat*0.017453293) *
COS(b.lat*0.017453293) *
POWER(SIN(((a.lng-b.lng)*0.017453293)/2),2))))) AS distance
FROM zips a, zips b WHERE
a.zip_code = ?
GROUP BY distance having distance <= ?";
my $sth = $dbh->prepare($query);
$sth->execute( $user_submitted_zip, $user_submitted_distance );
while( my ($zip, $distance) = $sth->fetchrow() ) ) {
# do something
}
This won't be that fast, but if you have a small record set ( less than 30k rows ) it should be fine. If you really want to go faster you should look into a search engine such as Sphinx which will do this for you.
fetchrow_array returns a list of list references, essentially a two-dimensional array, where each row represents a different result from the database query and each column represents a field from the query (in your case, there is only one field, or column, per row).
Calling while ($test123->fetchrow_array()) will cause an infinite loop as your program executes the query over and over again. If the query returns results, then the while condition will be satisfied and the loop will repeat. The usual idiom would be to say something more like for my $row ($test123->fetchrow_array()) { ..., which will only execute the query once and then iterate over the results.
Each result is a list reference, and the zip code you are interested in is in the first (and only) column, so you could accumulate the results in an array like this:
my #zips = (); # for final results
for my $row ($test123->fetchrow_array()) {
push #zips, $row->[0];
}
or even more concisely with Perl's map statement:
my #zips = map { $_->[0] } $test123->fetchrow_array()
which does the same thing.