0

我正在自学 CF 开发,并在学习时使用共享的 CF 托管站点进行测试。

我有一个包含 2500 多条数据记录的电子表格,大约 15 列。电子表格包含对该应用程序的基础至关重要的数据。它是离线维护的,并定期使用cffile. 上传后,电子表格将逐行导入 MySQL 表中。

问题是它花费的时间太长了。例如,它当前正在运行。在过去的 43 分钟内,只有 2500 条记录中的 192 条被添加到 MySQL 表中。有没有更有效的方法来做到这一点?

如果您查看下面的代码,它会在cfloop其中运行 cfspreadsheet 标记,并检索当前行。然后将该行作为“CSV”数据添加到 MySQL 表中。

关于如何更有效地做到这一点的任何建议?


<cfloop index="RecordNumber" from="2" to="#Records#" step="1"> 

    <cfspreadsheet action="read"
                format="csv"
                name="siteinfo"
                src="#floc#"
                headerrow="1"
                rows="#RecordNumber#" 
                columns="1-7,15,16,22,23"
                >

    <cfset ColumnCount = ListLen(siteinfo)>
    <cfset CSVSiteData = #siteinfo#>
    <cfset EntryType = "Site_Data">


    <cfquery name="Insertsite" datasource="TechData">
        INSERT INTO sitelist (EntryType, CSVSiteData, DateInserted)
        VALUES ('#EntryType#', '#CSVSiteDataf#', '#DateInserted#')
    </cfquery>

</cfloop>
4

2 回答 2

1

您每次迭代都读取整个 XLS 文件,只是为了获取一行。只需读取一次文件(在循环之前),然后遍历其行。

于 2014-09-13T23:47:03.423 回答
-1

是的,试图找到一种方法来避免每次通过都打开电子表格,这修复了它......现在快得多......

<body bgcolor="#0033FF">
    <cfset filenm = "SitesTest.xlsx">
    <cfset thisPath = GetDirectoryFromPath(ExpandPath("*.*"))>
    <cfset uploaddirectory = "#thisPath#Uploads\">
    <cfset uploadfile = "#uploaddirectory##filenm#">
    <cfset floc = "#uploadfile#">






<!--- --->
<!--- --->
<!--- --->


<!--- Read Spreadsheet Count number of Rows --->

<cfspreadsheet action="read"
                src="#floc#"
                headerrow="1"
                query="SiteSS"
                >

<cfset Records = "#SiteSS.RecordCount#">                
<!--- Get Header Row from Row 1 --->
<cfspreadsheet action="read"
                format="csv"
                name="ColumnHeaderRow"
                src="#floc#"
                headerrow="1"
                rows="1" 
                columns="1-7,15,16,22,23">

<cfset Headeritems = ListLen(ColumnHeaderRow)>

<cfset csvallVar = "">
<cfset CSVSiteData = "">


<cfspreadsheet 
        action = "read" 
        src="#floc#" 
        query="siteinfo" 
        rows="1-#Records#" 
        columns="1-7,15,16,22,23" >



<cfset rowcount = 2>

<cfloop Query="siteinfo" startrow="2" endrow="#Records#" > 

<cfset EntryType = "SiteInfo">

<!--- Create Line Item Lists--->
<cfset CSVSiteData = "#siteinfo.col_1#, #siteinfo.col_2#, #siteinfo.col_3#, #siteinfo.col_4#, #siteinfo.col_5#, #siteinfo.col_6#, #siteinfo.col_7#, #siteinfo.col_8#, #siteinfo.col_9#, #siteinfo.col_10#, #siteinfo.col_11#">

<cfset CSVSiteDataCheck = "#siteinfo.col_1#, #siteinfo.col_2#, #siteinfo.col_3#, #siteinfo.col_4#, #siteinfo.col_5#, #siteinfo.col_6#, #siteinfo.col_7#, #siteinfo.col_8#, #siteinfo.col_9#, #siteinfo.col_10#, #siteinfo.col_11#">

<cfset Manager = #siteinfo.col_1#>
<cfset Market = #siteinfo.col_2#>
<cfset FALocationCode = #siteinfo.col_3#>
<cfset USID = #siteinfo.col_4#>
<cfset CommonID = #siteinfo.col_5#>
<cfset EquipmentName = #siteinfo.col_6#>
<cfset EquipmentType = #siteinfo.col_7#>
<cfset PrimaryTech = #siteinfo.col_8#>
<cfset Address = #siteinfo.col_9#>
<cfset LatitudeDecimal = #siteinfo.col_10#>
<cfset LongitudeDecimal = #siteinfo.col_11#>



<!------>

<cfquery name="CheckDup" datasource="TechData">
    select CSVSiteData From sitelist2
    where CSVSiteData = '#CSVSiteDataCheck#'
</cfquery>


<cfif CheckDup.recordcount lt 1>

<cfquery name="Insertsite" datasource="TechData">
    INSERT INTO sitelist2 (
                            EntryType, 
                            CSVSiteData,
                            Manager, 
                            Market, 
                            FALocationCode, 
                            USID, 
                            CommonID, 
                            EquipmentName, 
                            EquipmentType, 
                            PrimaryTech, 
                            Address, 
                            LatitudeDecimal, 
                            LongitudeDecimal                            
                            )
    VALUES (
            '#EntryType#', 
            '#CSVSiteData#', 
            '#Manager#', 
            '#Market#', 
            '#FALocationCode#', 
            '#USID#', 
            '#CommonID#', 
            '#EquipmentName#', 
            '#EquipmentType#', 
            '#PrimaryTech#', 
            '#Address#', 
            #LatitudeDecimal#, 
            #LongitudeDecimal#
            )
</cfquery>



"Record <cfoutput>#rowcount#</cfoutput> added"<br />
<cfelse>
"Record <cfoutput>#rowcount#</cfoutput> not added"<br />
</cfif>




<cfset rowcount = rowcount + 1>
</cfloop>






</body>
于 2014-09-15T00:51:19.470 回答